All skills
gogf avatar

/goframe-v2

@911705d
by GoFramegogf/skills81 stars
3

GoFrame v2 development skill. Use only when the target Go project uses or is explicitly adopting GoFrame v2: the nearest go.mod requires github.com/gogf/gf/v2, existing Go files import github.com/gogf/gf/v2 or any github.com/gogf/gf/v2/... component package, or the user asks to scaffold, migrate, or build with GoFrame. Trigger for GoFrame-backed Go work such as APIs/controllers/services, middleware, routing/config, ORM/DAO/DO/entity/database operations, gf CLI/codegen, HTTP/gRPC services, and microservice conventions. Do not trigger for generic Go projects without GoFrame evidence, frontend-only work, shell scripts, or unrelated infrastructure tasks.

Use this Skill: https://skilld.dev/gh/gogf/skills/goframe-v2

This session only. Nothing lands on disk.

references核心组件模板引擎模板引擎-模板函数模板函数-基础函数.md

:::tip 以下为Golang标准库的一些基础语法和基础函数,GoFrame框架对部分基础函数做了必要的改进。 ::: 变量可以使用符号 | 在函数间传递

{{.value | Func1 | Func2}}

使用括号

{{printf "nums is %s %d" (printf "%d %d" 1 2) 3}}

and

{{and .X .Y .Z}}

and 会逐一判断每个参数,将返回第一个为空的参数,否则就返回最后一个非空参数

call

{{call .Field.Func .Arg1 .Arg2}}

call 可以调用函数,并传入参数

调用的函数需要返回 1 个值 或者 2 个值,返回两个值时,第二个值用于返回 error 类型的错误。返回的错误不等于 nil 时,执行将终止。

index

index 支持 map, slice, array, string,读取指定类型对应下标的值。

{{index .Maps "name"}}

len

{{printf "The content length is %d" (.Content|len)}}

返回对应类型的长度,支持类型: map, slice, array, string, chan。

not

not 返回输入参数的否定值。

例如,判断是否变量是否为空:

{{if not .Var}}
// 执行为空操作(.Var为空, 如: nil, 0, "", 长度为0的slice/map)
{{else}}
// 执行非空操作(.Var不为空)
{{end}}

or

{{or .X .Y .Z}}

or 会逐一判断每个参数,将返回第一个非空的参数,否则就返回最后一个参数。

print

同 fmt.Sprint。

printf

同 fmt.Sprintf。

println

同 fmt.Sprintln。

urlquery

{{urlquery "http://johng.cn"}}

将返回

http%3A%2F%2Fjohng.cn

eq / ne / lt / le / gt / ge

这类函数一般配合在 if 中使用

`eq`: arg1 == arg2
`ne`: arg1 != arg2
`lt`: arg1 < arg2
`le`: arg1 <= arg2
`gt`: arg1 > arg2
`ge`: arg1 >= arg2

eq 和其他函数不一样的地方是,支持多个参数。

{{eq arg1 arg2 arg3 arg4}}

和下面的逻辑判断相同:

arg1==arg2 || arg1==arg3 || arg1==arg4 ...

与 if 一起使用

{{if eq true .Var1 .Var2 .Var3}}...{{end}}
{{if lt 100 200}}...{{end}}

例如,判断变量不为空时执行:

{{if .Var}}
// 执行非空操作(.Var不为空)
{{else}}
// 执行为空操作(.Var为空, 如: nil, 0, "", 长度为0的slice/map)
{{end}}

对比函数改进

GoFrame 框架模板引擎对标准库自带的对比模板函数 eq/ne/lt/le/gt/ge 做了必要的改进,以便支持任意数据类型的比较。例如,在标准库模板中的以下比较:

{{eq 1 "1"}}

将会引发错误:

panic: template: at <eq 1 "1">: error calling eq: incompatible types for comparison

由于比较的两个参数不是同一数据类型,因此引发了 panic 错误。

在 GoFrame 框架的模板引擎中,将会自动将两个参数进行数据转换,转换为同一类型后再进行比较,这样的开发体验更好、灵活性更高。如果两个参数均为整型变量(或者整型字符串),那么将会转换为整型进行比较,否则转换为字符串变量进行比较(区分大小写)。

改进运行示例

我们来看一个 GoFrame 框架的模板引擎中的对比模板函数运行示例。

package main

import (
    "context"
    "fmt"
    "github.com/gogf/gf/v2/frame/g"
)

func main() {
    tplContent := `
eq:
eq "a" "a": {{eq "a" "a"}}
eq "1" "1": {{eq "1" "1"}}
eq  1  "1": {{eq  1  "1"}}

ne:
ne  1  "1": {{ne  1  "1"}}
ne "a" "a": {{ne "a" "a"}}
ne "a" "b": {{ne "a" "b"}}

lt:
lt  1  "2": {{lt  1  "2"}}
lt  2   2 : {{lt  2   2 }}
lt "a" "b": {{lt "a" "b"}}

le:
le  1  "2": {{le  1  "2"}}
le  2   1 : {{le  2   1 }}
le "a" "a": {{le "a" "a"}}

gt:
gt  1  "2": {{gt  1  "2"}}
gt  2   1 : {{gt  2   1 }}
gt "a" "a": {{gt "a" "a"}}

ge:
ge  1  "2": {{ge  1  "2"}}
ge  2   1 : {{ge  2   1 }}
ge "a" "a": {{ge "a" "a"}}
`
    content, err := g.View().ParseContent(context.TODO(), tplContent, nil)
    if err != nil {
        panic(err)
    }
    fmt.Println(content)
}

运行后,输出结果为:

eq:
eq "a" "a": true
eq "1" "1": true
eq  1  "1": true

ne:
ne  1  "1": false
ne "a" "a": false
ne "a" "b": true

lt:
lt  1  "2": true
lt  2   2 : false
lt "a" "b": true

le:
le  1  "2": true
le  2   1 : false
le "a" "a": true

gt:
gt  1  "2": false
gt  2   1 : true
gt "a" "a": false

ge:
ge  1  "2": false
ge  2   1 : true
ge "a" "a": true

Source: SKILL.md on GitHub

1 alert16d5 checks · Risk MEDIUM
  • Gen Agent Trust Hub16d

    The skill provides a comprehensive development guide and code examples for the GoFrame v2 framework. While highly informative for Go developers, it contains several examples with hardcoded placeholder secrets, tokens, and specific local file paths to sensitive configuration files (like Kubernetes credentials). These elements are provided for demonstration purposes but represent insecure practices if used in production without modification.

  • Socket16d

    5 alerts: gptSecurity, gptAnomaly

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    227/949 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 911705d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 months ago.

Steadyupdated 4 months ago
  • Go
  • Database
  • goframe
  • orm
  • http
  • grpc
  • microservices
  • crud
  • scaffolding
  • middleware

README badge

README badge for gogf/skills

Provides instruction and conventions for developing with GoFrame v2, a modular Go framework for building HTTP and gRPC services, microservices, and database operations using its ORM, CLI tooling, and project scaffolding. Use this skill when the target project has GoFrame v2 as a dependency or when the user requests scaffolding, migrations, or service development with GoFrame conventions.

Generated from the current SKILL.md.

When should I use this skill?
Use this skill only when your Go project uses GoFrame v2, indicated by github.com/gogf/gf/v2 in go.mod or imports. Do not use for generic Go projects, frontend work, or infrastructure tasks unrelated to GoFrame.
Should I manually set created_at and updated_at fields?
No. GoFrame automatically writes created_at on insert and updated_at on insert/update/save. Manually setting these fields is redundant and violates project conventions.
What should I use for database operations instead of g.Map?
Always use DO objects from internal/model/do/. DO struct fields are interface{}, unset fields remain nil and are automatically ignored by the ORM.
How does soft delete work in GoFrame?
Call Delete() on the DAO; GoFrame automatically converts it to UPDATE SET deleted_at = NOW(). Queries automatically filter out soft-deleted rows without requiring manual WhereNull conditions.
Where should I implement business logic?
Implement business logic directly in the service/ directory. Do not use the logic/ directory unless explicitly requested.

Generated from the current SKILL.md. These answers refresh after source changes.