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

gcmd 组件提供了常用的基础包方法,可以按照默认的解析规则,直接获取命令行参数及选项。

常用方法

更多组件方法请参考接口文档: https://pkg.go.dev/github.com/gogf/gf/v2/os/gcmd

func Init(args ...string)

func GetArg(index int, def ...string) *gvar.Var
func GetArgAll() []string

func GetOpt(name string, def ...string) *gvar.Var
func GetOptAll() map[string]string

Init 自定义命令行

默认情况下,gcmd 组件会自动从 os.Args 解析获取参数及数据。我们可以通过 Init 方法自定义命令行数据。使用示例:

func ExampleInit() {
    gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
    fmt.Printf(`%#v`, gcmd.GetArgAll())

    // Output:
    // []string{"gf", "build", "main.go"}
}

GetArg 参数获取

参数获取可以通过以下两个方法:

  1. GetArg 方法用以获取默认解析的命令行参数,参数通过输入索引位置获取,索引位置从 0 开始,但往往我们需要获取的参数是从 1 开始,因为索引 0 的参数是程序名称。
  2. GetArgAll 方法用于获取所有的命令行参数。

使用示例:

func ExampleGetArg() {
    gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
    fmt.Printf(
        `Arg[0]: "%v", Arg[1]: "%v", Arg[2]: "%v", Arg[3]: "%v"`,
        gcmd.GetArg(0), gcmd.GetArg(1), gcmd.GetArg(2), gcmd.GetArg(3),
    )

    // Output:
    // Arg[0]: "gf", Arg[1]: "build", Arg[2]: "main.go", Arg[3]: ""
}

func ExampleGetArgAll() {
    gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
    fmt.Printf(`%#v`, gcmd.GetArgAll())

    // Output:
    // []string{"gf", "build", "main.go"}
}

GetOpt 选项获取

选项获取可以通过以下两个方法:

  1. GetOpt 方法用以获取默认解析的命令行选项,选项通过名称获取,并且选项的输入没有顺序性,可以输入到任意的命令行位置。当给定名称的选项数据不存在时,返回 nil。注意判断不带数据的选项是否存在时,可以通过 GetOpt(name) != nil 方式。
  2. GetOptAll 方法用于获取所有的选项。

使用示例:

func ExampleGetOpt() {
    gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
    fmt.Printf(
        `Opt["o"]: "%v", Opt["y"]: "%v", Opt["d"]: "%v"`,
        gcmd.GetOpt("o"), gcmd.GetOpt("y"), gcmd.GetOpt("d", "default value"),
    )

    // Output:
    // Opt["o"]: "gf.exe", Opt["y"]: "", Opt["d"]: "default value"
}

func ExampleGetOptAll() {
    gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
    fmt.Printf(`%#v`, gcmd.GetOptAll())

    // May Output:
    // map[string]string{"o":"gf.exe", "y":""}
}

GetOptWithEnv 特性

func GetOptWithEnv(key string, def ...interface{}) *gvar.Var

该方法用于获取命令行中指定的选项数值,如果该选项不存在时,则从环境变量中读取。但是两者的名称规则会不一样。例如: gcmd.GetOptWithEnv("gf.debug") 将会优先去读取命令行中的 gf.debug 选项,当不存在时,则会去读取 GF_DEBUG 环境变量。

需要注意的是参数命名转换规则:

  • 环境变量会将名称转换为大写,名称中的 . 字符转换为 _ 字符。
  • 命令行中会将名称转换为小写,名称中的 _ 字符转换为 . 字符。

使用示例:

func ExampleGetOptWithEnv() {
    fmt.Printf("Opt[gf.test]:%s\n", gcmd.GetOptWithEnv("gf.test"))
    _ = genv.Set("GF_TEST", "YES")
    fmt.Printf("Opt[gf.test]:%s\n", gcmd.GetOptWithEnv("gf.test"))

    // Output:
    // Opt[gf.test]:
    // Opt[gf.test]:YES
}

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.