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链式操作ORM链式操作-事务处理.md

Model 对象也可以通过 TX 事务接口创建,通过事务对象创建的 Model 对象与通过 DB 数据库对象创建的 Model 对象功能是一样的,只不过前者的所有操作都是基于事务,而当事务提交或者回滚后,对应的 Model 对象不能被继续使用,否则会返回错误。因为该 TX 接口不能被继续使用,一个事务对象仅对应于一个事务流程, Commit/ Rollback 后即结束。

本章节仅对链式操作涉及到的事务处理方法做简单介绍,更详细的介绍请参考 ORM事务处理 章节。

示例1,通过 Transaction

为方便事务操作, gdb 提供了事务的闭包操作,通过 Transaction 方法实现,该方法定义如下:

func (db DB) Transaction(ctx context.Context, f func(ctx context.Context, tx TX) error) (err error)

当给定的闭包方法返回的 error 为 nil 时,那么闭包执行结束后当前事务自动执行 Commit 提交操作;否则自动执行 Rollback 回滚操作。 :::tip 如果闭包内部操作产生 panic 中断,该事务也将进行回滚。 :::

func Register() error {
    return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
        var (
            result sql.Result
            err    error
        )
        // 写入用户基础数据
        result, err = tx.Table("user").Insert(g.Map{
            "name":  "john",
            "score": 100,
            //...
        })
        if err != nil {
            return err
        }
        // 写入用户详情数据,需要用到上一次写入得到的用户uid
        result, err = tx.Table("user_detail").Insert(g.Map{
            "uid":   result.LastInsertId(),
            "phone": "18010576258",
            //...
        })
        return err
    })
}

示例2,通过 TX 链式操作

我们也可以在链式操作中通过 TX 方法切换绑定的事务对象。多次链式操作可以绑定同一个事务对象,在该事务对象中执行对应的链式操作。

func Register() error {
    var (
        uid int64
        err error
    )
    tx, err := g.DB().Begin()
    if err != nil {
        return err
    }
    // 方法退出时检验返回值,
    // 如果结果成功则执行tx.Commit()提交,
    // 否则执行tx.Rollback()回滚操作。
    defer func() {
        if err != nil {
            tx.Rollback()
        } else {
            tx.Commit()
        }
    }()
    // 写入用户基础数据
    uid, err = AddUserInfo(tx, g.Map{
        "name":  "john",
        "score": 100,
        //...
    })
    if err != nil {
        return err
    }
    // 写入用户详情数据,需要用到上一次写入得到的用户uid
    err = AddUserDetail(tx, g.Map{
        "uid":   uid,
        "phone": "18010576259",
        //...
    })
    return err
}

func AddUserInfo(tx gdb.TX, data g.Map) (int64, error) {
    result, err := g.Model("user").TX(tx).Data(data).Insert()
    if err != nil {
        return 0, err
    }
    uid, err := result.LastInsertId()
    if err != nil {
        return 0, err
    }
    return uid, nil
}

func AddUserDetail(tx gdb.TX, data g.Map) error {
    _, err := g.Model("user_detail").TX(tx).Data(data).Insert()
    return err
}

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.