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

≈1.5k tokens on demand. Your agent reads this file only when SKILL.md points to it.

基本介绍

GoFrame 框架提供了功能强大的配置管理组件,采用了解耦化和接口化设计,可以灵活对接各种第三方配置管理中心。配置管理组件默认提供基于本地系统文件的实现,更多的第三方配置中心实现请参考社区组件: https://github.com/gogf/gf/tree/master/contrib/config

社区组件提供了常用的多种配置中心实现,例如 Polaris、 Apollo、 Nacos、 Consul 以及容器编排的 Kubernetes ConfigMap。

组件启用

配置管理组件的启用采用了包初始化的方式。由于配置管理功能比较底层,需要保证社区组件包在 main 包的最顶部引入,确保配置组件在其他组件之前初始化。我们这里以 Polaris 为例,社区组件的使用说明: https://github.com/gogf/gf/tree/master/contrib/config/polaris

需要提供一个独立的引入包,例如 boot:

package boot

import (
    "github.com/gogf/gf/contrib/config/polaris/v2"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gctx"
)

func init() {
    var (
        ctx       = gctx.GetInitCtx()
        namespace = "default"
        fileGroup = "TestGroup"
        fileName  = "config.yaml"
        path      = "manifest/config/polaris.yaml"
        logDir    = "/tmp/polaris/log"
    )
    // Create polaris Client that implements gcfg.Adapter.
    adapter, err := polaris.New(ctx, polaris.Config{
        Namespace: namespace,
        FileGroup: fileGroup,
        FileName:  fileName,
        Path:      path,
        LogDir:    logDir,
        Watch:     true,
    })
    if err != nil {
        g.Log().Fatalf(ctx, `%+v`, err)
    }
    // Change the adapter of default configuration instance.
    g.Cfg().SetAdapter(adapter)
}

其中配置参数说明:

  • Namespace 指定的是 Polaris 配置中的命名空间。
  • FileGroup 指定的是 Polaris 中的文件分组。
  • FileName 指定的是需要读取的 Polaris 中的配置文件名称。
  • Path 指定的是 Polaris 服务端配置文件路径,包括 Polaris 的链接地址、监听地址、组件输出日志路径等。

Polaris 的服务端配置文件内容如下:

global:
  serverConnector:
    addresses:
      - 127.0.0.1:8091
config:
  configConnector:
    addresses:
      - 127.0.0.1:8093
consumer:
  localCache:
    persistDir: "/tmp/polaris/backup"

随后在 main.go 顶部引入 boot 包,保证该包的引入在所有组件的最前面:

package main

import (
    _ "github.com/gogf/gf/example/config/polaris/boot"

    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gctx"
)

func main() {
    var ctx = gctx.GetInitCtx()

    // Available checks.
    g.Dump(g.Cfg().Available(ctx))

    // All key-value configurations.
    g.Dump(g.Cfg().Data(ctx))

    // Retrieve certain value by key.
    g.Dump(g.Cfg().MustGet(ctx, "server.address"))
}

注意其中顶部 import 的引入语句:

_ "github.com/gogf/gf/example/config/polaris/boot"

常用组件

组件名称 文档说明 备注
file 框架内置 默认实现
apollo https://github.com/gogf/gf/tree/master/contrib/config/apollo
kubecm https://github.com/gogf/gf/tree/master/contrib/config/kubecm 常用于容器部署环境
nacos https://github.com/gogf/gf/tree/master/contrib/config/nacos
polaris https://github.com/gogf/gf/tree/master/contrib/config/polaris
consul https://github.com/gogf/gf/tree/master/contrib/config/consul

更多组件,请参考: https://github.com/gogf/gf/tree/master/contrib/config

使用示例

https://github.com/gogf/gf/tree/master/example/config/polaris

运行 polaris

docker run -d --name polaris -p 8080:8080 -p 8090:8090 -p 8091:8091 -p 8093:8093 loads/polaris-server-standalone:1.11.2

运行示例

$ go run main.go
true
{}
"failed to update local value: config file is empty"
panic: failed to update local value: config file is empty

goroutine 1 [running]:
github.com/gogf/gf/v2/os/gcfg.(*Config).MustGet(0x0?, {0x1c1c4f8?, 0xc0000c2000?}, {0x1ac11ad?, 0x0?}, {0x0?, 0xc000002340?, 0xc000064738?})
        /Users/john/Workspace/gogf/gf/os/gcfg/gcfg.go:167 +0x5e
main.main()
        /Users/john/Workspace/gogf/gf/example/config/polaris/main.go:20 +0x1b8

可以看到最后的 MustGet 方法执行报错,这是因为 Polaris 配置中心里并没有指定的命名空间、配置分组以及配置文件。即使查询不到任何数据,但由于涉及到配置问题因此返回了错误。由于这里使用的是 Must* 方法,在执行返回错误时将会直接 panic 而不是返回错误。

那么我们进入 polaris 的后台添加一些测试数据再试试。

添加测试数据

进入 http://127.0.0.1:8080/#/login 默认账号 polaris 密码 polaris

再次运行示例

$ go run main.go
true
{
    "server": {
        "openapiPath": "/api.json",
        "swaggerPath": "/swagger",
        "address":     ":8199",
    },
}
<nil>
":8199"

可以看到已经正确查询到了 Polaris 配置中心中的配置数据。

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.