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核心组件数据库ORMORM最佳实践利用指针属性和do对象实现灵活的修改接口.md

大家都知道框架自带的开发工具可以生成 do 对象代码,该 do 对象主要用于查询、修改、写入等操作时对操作字段的自动 nil 过滤。

今天教给大家一个新的玩法,通过指针结合 do 对象快速实现灵活、便捷的修改操作 API 实现。

数据结构

以下是我们使用的用户表数据结构:

CREATE TABLE `user`(
    `id`        int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户ID',
    `passport`  varchar(32) NOT NULL COMMENT '账号',
    `password`  varchar(32) NOT NULL COMMENT '密码',
    `nickname`  varchar(32) NOT NULL COMMENT '昵称',
    `status`    varchar(32) NOT NULL COMMENT '状态',
    `brief`     varchar(512) NOT NULL COMMENT '备注信息',
    `create_at` datetime DEFAULT NULL COMMENT '创建时间',
    `update_at` datetime DEFAULT NULL COMMENT '修改时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uniq_passport` (`passport`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

其中的用户状态,我们使用了单独的类型定义,用以实现枚举值:

type Status string

const (
    StatusEnabled  Status = "enabled"
    StatusDisabled Status = "disabled"
)

通过 gf gen dao 命令,我们自动生成的 do 对象如下:

type User struct {
    g.Meta    `table:"user" orm:"do:true"`
    Id        interface{}
    Passport  interface{}
    Password  interface{}
    Nickname  interface{}
    Status    interface{}
    Brief     interface{}
    CreatedAt interface{}
    UpdatedAt interface{}
}

请求API定义

我们来实现一个用户信息修改的 API 接口,这是一个运维管理接口,可以通过用户账号名称来修改用户信息。该 API 的定义如下:

type UpdateReq struct {
    g.Meta   `path:"/user/{Id}" method:"post" summary:"修改用户信息"`
    Passport string  `v:"required" dc:"用户账号"`
    Password *string `dc:"修改用户密码"`
    Nickname *string `dc:"修改用户昵称"`
    Status   *Status `dc:"修改用户状态"`
    Brief    *string `dc:"修改用户描述"`
}

其中,用户的可修改信息为密码、昵称、状态和描述,可能同时修改一项或者多项。这里使用了 指针类型 的属性参数, 用于实现:当传递该参数时执行修改,不传递时不修改。

业务逻辑实现

为了简化实例,我们这里直接在控制器中将指针参数传递给 do 对象。我们知道当调用端没有传递该参数时,该参数为 nil,那么传递给 do 对象的字段时,仍然是 nil,这个时候执行数据库更新操作时, do 对象中的 nil 字段将会被自动过滤掉。

func (c *Controller) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
    _, err = dao.User.Ctx(ctx).Data(do.User{
        Password: req.Password,
        Nickname: req.Nickname,
        Status:   req.Status,
        Brief:    req.Brief,
    }).Where(do.User{
        Passport: req.Passport,
    }).Update()
    return
}

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.