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 以下常用方法列表,文档更新可能滞后于代码新特性,更多的方法及示例请参考代码文档: https://pkg.go.dev/github.com/gogf/gf/v2/os/gcfg :::

GetWithEnv

  • 说明
    • GetWithEnv方法会先从默认的配置文件中获取配置数据,获取为空的时候,将会去当前的环境变量中进行获取。需要注意的是名称命名转换规则:
    • 环境变量会将名称转换为大写,名称中的.字符转换为_字符。
    • 参数名称中会将名称转换为小写,名称中的_字符转换为.字符。
  • 格式:
GetWithEnv(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
  • 示例:
func ExampleConfig_GetWithEnv() {
      var (
          key = `env.test`
          ctx = gctx.New()
      )
      v, err := g.Cfg().GetWithEnv(ctx, key)
      if err != nil {
          panic(err)
      }
      fmt.Printf("env:%s\n", v)
      if err = genv.Set(`ENV_TEST`, "gf"); err != nil {
          panic(err)
      }
      v, err = g.Cfg().GetWithEnv(ctx, key)
      if err != nil {
          panic(err)
      }
      fmt.Printf("env:%s", v)

      // Output:
      // env:
      // env:gf
}

GetWithCmd

  • 说明:GetWithCmd方法与GetWithEnv方法类似,也是先从默认的配置对象中获取配置数据,但是获取为空的时候,是去命令行中获取配置信息。
  • 格式:
GetWithCmd(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
  • 示例:
func ExampleConfig_GetWithCmd() {
      var (
          key = `cmd.test`
          ctx = gctx.New()
      )
      v, err := g.Cfg().GetWithCmd(ctx, key)
      if err != nil {
          panic(err)
      }
      fmt.Printf("cmd:%s\n", v)
      // Re-Initialize custom command arguments.
      os.Args = append(os.Args, fmt.Sprintf(`--%s=yes`, key))
      gcmd.Init(os.Args...)
      // Retrieve the configuration and command option again.
      v, err = g.Cfg().GetWithCmd(ctx, key)
      if err != nil {
          panic(err)
      }
      fmt.Printf("cmd:%s", v)

      // Output:
      // cmd:
      // cmd:yes
}

MustGetWithCmd

  • 说明:MustGetWithCmd方法与GetWithCmd方法类似,该方法只会返回配置内容,一旦内部发生任何错误,将会有panic。
  • 格式:
MustGetWithCmd(ctx context.Context, pattern string, def ...interface{}) *gvar.Var
  • 示例:
func ExampleConfig_MustGetWithCmd() {
      var (
          key = `cmd.test`
          ctx = gctx.New()
      )
      v := g.Cfg().MustGetWithCmd(ctx, key)

      fmt.Printf("cmd:%s\n", v)
      // Re-Initialize custom command arguments.
      os.Args = append(os.Args, fmt.Sprintf(`--%s=yes`, key))
      gcmd.Init(os.Args...)
      // Retrieve the configuration and command option again.
      v = g.Cfg().MustGetWithCmd(ctx, key)

      fmt.Printf("cmd:%s", v)

      // Output:
      // cmd:
      // cmd:yes
}

MustGetWithEnv

  • 说明: MustGetWithEnv 方法与 GetWithEnv 方法类似,该方法只会返回配置内容,一旦内部发生任何错误,将会有 panic。
  • 格式:
MustGetWithEnv(ctx context.Context, pattern string, def ...interface{}) *gvar.Var
  • 示例:
func ExampleConfig_MustGetWithEnv() {
      var (
          key = `env.test`
          ctx = gctx.New()
      )
      v := g.Cfg().MustGetWithEnv(ctx, key)

      fmt.Printf("env:%s\n", v)
      if err := genv.Set(`ENV_TEST`, "gf"); err != nil {
          panic(err)
      }
      v = g.Cfg().MustGetWithEnv(ctx, key)

      fmt.Printf("env:%s", v)

      // Output:
      // env:
      // env:gf
}

Data

  • 说明: Data 方法从配置对象中获取配置数据,组装成 map[string]interface{} 类型。
  • 格式:
Data(ctx context.Context) (data map[string]interface{}, err error)
  • 示例:
func ExampleConfig_Data() {
      ctx := gctx.New()
      content := `
v1    = 1
v2    = "true"
v3    = "off"
v4    = "1.23"
array = [1,2,3]
[redis]
      disk  = "127.0.0.1:6379,0"
      cache = "127.0.0.1:6379,1"
`
      c, err := gcfg.New()
      if err != nil{
          panic(err)
      }
      c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
      data, err := c.Data(ctx)
      if err != nil{
          panic(err)
      }

      fmt.Println(data)

      // Output:
      // map[array:[1 2 3] redis:map[cache:127.0.0.1:6379,1 disk:127.0.0.1:6379,0] v1:1 v2:true v3:off v4:1.23]
}

MustData

  • 说明: MustData 方法从配置对象中获取配置数据,组装成 map[string]interface{} 类型。当该方法内部产生错误时不会返回错误,而是直接 panic。
  • 格式:
MustData(ctx context.Context) map[string]interface{}
  • 示例:
func ExampleConfig_MustData() {
      ctx := gctx.New()
      content := `
v1    = 1
v2    = "true"
v3    = "off"
v4    = "1.23"
array = [1,2,3]
[redis]
      disk  = "127.0.0.1:6379,0"
      cache = "127.0.0.1:6379,1"
`
      c, err := gcfg.New()
      if err != nil{
          panic(err)
      }

      c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
      data := c.MustData(ctx)

      fmt.Println(data)

      // Output:
      // map[array:[1 2 3] redis:map[cache:127.0.0.1:6379,1 disk:127.0.0.1:6379,0] v1:1 v2:true v3:off v4:1.23]
}

Get

  • 说明: Get 方法从配置对象中获取配置数据,返回 gvar 泛型对象。
  • 格式:
Get(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
  • 示例:
func ExampleConfig_Get() {
      ctx := gctx.New()
      content := `
v1    = 1
v2    = "true"
v3    = "off"
v4    = "1.23"
array = [1,2,3]
[redis]
      disk  = "127.0.0.1:6379,0"
      cache = "127.0.0.1:6379,1"
`
      c, err := gcfg.New()
      if err != nil{
          panic(err)
      }

      c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
      data,err := c.Get(ctx,"redis")

      if err != nil {
          panic(err)
      }
      fmt.Println(data)

      // Output:
      // {"cache":"127.0.0.1:6379,1","disk":"127.0.0.1:6379,0"}
}

MustGet

  • 说明: MustGet 方法与 Get 类似,也是配置对象中获取配置数据,组装成 gvar 结构,但是返回参数只有一个: *gvar.Var
  • 注意:当配置文件不存在或者是其他 error 的情况下,会直接 panic,需要做好异常捕获措施。
  • 格式:
MustGet(ctx context.Context, pattern string, def ...interface{}) *gvar.Var
  • 示例:
func ExampleConfig_MustGet() {
      ctx := gctx.New()
      content := `
v1    = 1
v2    = "true"
v3    = "off"
v4    = "1.23"
array = [1,2,3]
[redis]
      disk  = "127.0.0.1:6379,0"
      cache = "127.0.0.1:6379,1"
`
      c, err := gcfg.New()
      if err != nil{
          panic(err)
      }

      c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
      data := c.MustGet(ctx,"redis")

      fmt.Println(data)

      // Output:
      // {"cache":"127.0.0.1:6379,1","disk":"127.0.0.1:6379,0"}
}

GetAdapter

  • 说明: GetAdapter 方法是获取当前运行的 gcfg 适配器信息,关于适配器相关,可以请点击 配置管理-接口化设计
  • 格式:
GetAdapter() Adapter
  • 示例:
func ExampleConfig_GetAdapter() {
      c, err := gcfg.New()
      if err != nil{
          panic(err)
      }

      adapter := c.GetAdapter()
      fmt.Println(adapter)

      // Output:
      // &{config.toml 0xc00014d720 0xc000371880 false}
}

SetAdapter

  • 说明: SetAdapter 方法是设置当前运行的 gcfg 适配器信息,关于适配器相关,可以请点击 配置管理-接口化设计
  • 格式:
SetAdapter(adapter Adapter)
  • 示例:
func ExampleConfig_SetAdapter() {
      c, err := gcfg.New()
      if err != nil{
          panic(err)
      }

      adapter := c.GetAdapter()
      c.SetAdapter(adapter)
      fmt.Println(adapter)

      // Output:
      // &{config.toml 0xc00014d720 0xc000371880 false}
}

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.