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.9k tokens on demand. Your agent reads this file only when SKILL.md points to it.

GoFrame 框架封装了一些常用的数据类型以及对象获取方法,通过 g.* 方法获取。 :::tip g 是一个强耦合的模块,目的是为开发者在对频繁使用的类型/对象调用时提供便利。 ::: 使用方式:

import "github.com/gogf/gf/v2/frame/g"

数据类型

常用数据类型别名。

type (
    Var = gvar.Var        // Var is a universal variable interface, like generics.
    Ctx = context.Context // Ctx is alias of frequently-used context.Context.
)

type (
    Map        = map[string]interface{}      // Map is alias of frequently-used map type map[string]interface{}.
    MapAnyAny  = map[interface{}]interface{} // MapAnyAny is alias of frequently-used map type map[interface{}]interface{}.
    MapAnyStr  = map[interface{}]string      // MapAnyStr is alias of frequently-used map type map[interface{}]string.
    MapAnyInt  = map[interface{}]int         // MapAnyInt is alias of frequently-used map type map[interface{}]int.
    MapStrAny  = map[string]interface{}      // MapStrAny is alias of frequently-used map type map[string]interface{}.
    MapStrStr  = map[string]string           // MapStrStr is alias of frequently-used map type map[string]string.
    MapStrInt  = map[string]int              // MapStrInt is alias of frequently-used map type map[string]int.
    MapIntAny  = map[int]interface{}         // MapIntAny is alias of frequently-used map type map[int]interface{}.
    MapIntStr  = map[int]string              // MapIntStr is alias of frequently-used map type map[int]string.
    MapIntInt  = map[int]int                 // MapIntInt is alias of frequently-used map type map[int]int.
    MapAnyBool = map[interface{}]bool        // MapAnyBool is alias of frequently-used map type map[interface{}]bool.
    MapStrBool = map[string]bool             // MapStrBool is alias of frequently-used map type map[string]bool.
    MapIntBool = map[int]bool                // MapIntBool is alias of frequently-used map type map[int]bool.
)

type (
    List        = []Map        // List is alias of frequently-used slice type []Map.
    ListAnyAny  = []MapAnyAny  // ListAnyAny is alias of frequently-used slice type []MapAnyAny.
    ListAnyStr  = []MapAnyStr  // ListAnyStr is alias of frequently-used slice type []MapAnyStr.
    ListAnyInt  = []MapAnyInt  // ListAnyInt is alias of frequently-used slice type []MapAnyInt.
    ListStrAny  = []MapStrAny  // ListStrAny is alias of frequently-used slice type []MapStrAny.
    ListStrStr  = []MapStrStr  // ListStrStr is alias of frequently-used slice type []MapStrStr.
    ListStrInt  = []MapStrInt  // ListStrInt is alias of frequently-used slice type []MapStrInt.
    ListIntAny  = []MapIntAny  // ListIntAny is alias of frequently-used slice type []MapIntAny.
    ListIntStr  = []MapIntStr  // ListIntStr is alias of frequently-used slice type []MapIntStr.
    ListIntInt  = []MapIntInt  // ListIntInt is alias of frequently-used slice type []MapIntInt.
    ListAnyBool = []MapAnyBool // ListAnyBool is alias of frequently-used slice type []MapAnyBool.
    ListStrBool = []MapStrBool // ListStrBool is alias of frequently-used slice type []MapStrBool.
    ListIntBool = []MapIntBool // ListIntBool is alias of frequently-used slice type []MapIntBool.
)

type (
    Slice    = []interface{} // Slice is alias of frequently-used slice type []interface{}.
    SliceAny = []interface{} // SliceAny is alias of frequently-used slice type []interface{}.
    SliceStr = []string      // SliceStr is alias of frequently-used slice type []string.
    SliceInt = []int         // SliceInt is alias of frequently-used slice type []int.
)

type (
    Array    = []interface{} // Array is alias of frequently-used slice type []interface{}.
    ArrayAny = []interface{} // ArrayAny is alias of frequently-used slice type []interface{}.
    ArrayStr = []string      // ArrayStr is alias of frequently-used slice type []string.
    ArrayInt = []int         // ArrayInt is alias of frequently-used slice type []int.
)

常用对象

常用对象往往通过 单例模式 进行管理,可以根据不同的单例名称获取对应的对象实例,并在对象初始化时会自动检索获取配置文件中的对应配置项,具体配置项请查看对应对象的章节介绍。 :::info 注意事项:在运行时阶段,每一次通过 g 模块获取单例对象时都会有内部全局锁机制来保证操作和数据的并发安全性,原理性上来讲在并发量大的场景下会存在锁竞争的情况,但绝大部分的业务场景下开发者均不需要太在意锁竞争带来的性能损耗。此外,开发者也可以通过将获取到的单例对象保存到特定的模块下的内部变量重复使用,以此避免运行时锁竞争情况。 :::

HTTP 客户端对象

func Client() *ghttp.Client

创建一个新的 HTTP 客户端对象。

Validator 校验对象

func Validator() *gvalid.Validator

创建一个新的数据校验对象。

(单例) 配置管理对象

func Cfg(name ...string) *gcfg.Config

该单例对象将会自动按照文件后缀 toml/yaml/yml/json/ini/xml/properties 文自动检索配置文件。默认情况下会自动检索以下配置文件:

  • config
  • config.toml
  • config.yaml
  • config.yml
  • config.json
  • config.ini
  • config.xml
  • config.properties

并缓存,配置文件在外部被修改时将会自动刷新缓存。

为方便多文件场景下的配置文件调用,简便使用并提高开发效率,单例对象在创建时将会自动使用单例名称进行文件检索。例如: g.Cfg("redis") 获取到的单例对象将默认会自动检索以下文件:

  • redis
  • redis.toml
  • redis.yaml
  • redis.yml
  • redis.json
  • redis.ini
  • redis.xml
  • redis.properties

如果检索成功那么将该文件加载到内存缓存中,下一次将会直接从内存中读取;当该文件不存在时,则使用默认的配置文件( config.toml)。

(单例) 日志管理对象

func Log(name ...string) *glog.Logger

该单例对象将会自动读取默认配置文件中的 logger 配置项,并只会初始化一次日志对象。

(单例) 模板引擎对象

func View(name ...string) *gview.View

该单例对象将会自动读取默认配置文件中的 viewer 配置项,并只会初始化一次模板引擎对象。内部采用了 懒初始化 设计,获取模板引擎对象时只是创建了一个轻量的模板管理对象,只有当解析模板文件时才会真正初始化。

(单例) WEB Server

func Server(name ...interface{}) *ghttp.Server

该单例对象将会自动读取默认配置文件中的 server 配置项,并只会初始化一次 Server 对象。

(单例) TCP Server

func TcpServer(name ...interface{}) *gtcp.Server

(单例) UDP Server

func UdpServer(name ...interface{}) *gudp.Server

(单例) 数据库 ORM 对象

func DB(name ...string) *gdb.Db

该单例对象将会自动读取默认配置文件中的 database 配置项,并只会初始化一次 DB 对象。

此外,可以通过以下方法在默认数据库上创建一个 Model 对象:

func Model(tables string, db ...string) *gdb.Model

(单例) Redis 客户端对象

func Redis(name ...string) *gredis.Redis

该单例对象将会自动读取默认配置文件中的 redis 配置项,并只会初始化一次 Redis 对象。

(单例) 资源管理对象

func Res(name ...string) *gres.Resource

(单例) 国际化管理对象

func I18n(name ...string) *gi18n.Manager

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.