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项目脚手架接口开发Step5 - 完成接口逻辑实现.md

可以看到,通过项目脚手架工具,很多与项目业务逻辑无关的代码都已经预先生成好,我们只需要关注业务逻辑实现即可。我们接下来看看如何实现CRUD具体逻辑吧。

创建接口

创建逻辑实现

package user

import (
    "context"

    "demo/api/user/v1"
    "demo/internal/dao"
    "demo/internal/model/do"
)

func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
    insertId, err := dao.User.Ctx(ctx).Data(do.User{
        Name:   req.Name,
        Status: v1.StatusOK,
        Age:    req.Age,
    }).InsertAndGetId()
    if err != nil {
        return nil, err
    }
    res = &v1.CreateRes{
        Id: insertId,
    }
    return
}

在Create实现方法中:

  • 我们通过dao.User通过dao组件操作user表。
  • 每个dao操作都需要传递ctx参数,因此我们通过Ctx(ctx)方法创建一个gdb.Model对象,该对象是框架的模型对象,用于操作特定的数据表。
  • 通过Data传递需要写入数据表的数据,我们这里使用do转换模型对象输入我们的数据。do转换模型会自动过滤nil数据,并在底层自动转换为对应的数据表字段类型。在绝大部分时候,我们都使用do转换模型来给数据库操作对象传递写入/更新参数、查询条件等数据。
  • 通过InsertAndGetId方法将Data的参数写入数据库,并返回新创建的记录主键id。

参数校验实现

等等,大家可能会问,为什么这里没有校验逻辑呢?因为校验逻辑都已经配置到请求参数对象CreateReq上了。还记得前面介绍的v标签吗?我们再来看看这个请求参数对象:

type CreateReq struct {
    g.Meta `path:"/user" method:"put" tags:"User" summary:"Create user"`
    Name   string `v:"required|length:3,10" dc:"user name"`
    Age    uint   `v:"required|between:18,200" dc:"user age"`
}
type CreateRes struct {
    Id int64 `json:"id" dc:"user id"`
}

这里的required/length/between校验规则在调用路由函数Create之前就已经由GoFrame框架的Server自动执行了。 如果请求参数校验失败,会立即返回错误,不会进入到路由函数。GoFrame框架的这种机制极大地简便了开发流程, 开发者在这个路由函数中,仅需要关注业务逻辑实现即可。 :::info 当然,如果有一些额外的、定制化的业务逻辑校验,是需要在路由函数中自行实现的哟。 :::

删除接口

package user

import (
    "context"

    "demo/api/user/v1"
    "demo/internal/dao"
)

func (c *ControllerV1) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) {
    _, err = dao.User.Ctx(ctx).WherePri(req.Id).Delete()
    return
}

删除逻辑比较简单,我们这里用到一个WherePri方法,该方法会将给定的参数req.Id作为主键进行Where条件限制。

更新接口

package user

import (
    "context"

    "demo/api/user/v1"
    "demo/internal/dao"
    "demo/internal/model/do"
)

func (c *ControllerV1) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
    _, err = dao.User.Ctx(ctx).Data(do.User{
        Name:   req.Name,
        Status: req.Status,
        Age:    req.Age,
    }).WherePri(req.Id).Update()
    return
}

更新接口也比较简单,除了已经介绍过的WherePri方法,在更新数据时也需要通过Data方法传递更新的数据。

查询接口(单个)

package user

import (
    "context"

    "demo/api/user/v1"
    "demo/internal/dao"
)

func (c *ControllerV1) GetOne(ctx context.Context, req *v1.GetOneReq) (res *v1.GetOneRes, err error) {
    res = &v1.GetOneRes{}
    err = dao.User.Ctx(ctx).WherePri(req.Id).Scan(&res.User)
    return
}

数据查询接口中,我们使用了Scan方法,该方法可以将查询到的单条数据表记录智能地映射到结构体对象上。大家需要注意这里的&res.User中的User属性对象其实是没有初始化的,其值为nil。如果查询到了数据,Scan方法会对其做初始化并赋值,如果查询不到数据,那么Scan方法什么都不会做,其值还是nil。

查询接口(列表)

package user

import (
    "context"

    "demo/api/user/v1"
    "demo/internal/dao"
    "demo/internal/model/do"
)

func (c *ControllerV1) GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error) {
    res = &v1.GetListRes{}
    err = dao.User.Ctx(ctx).Where(do.User{
        Age:    req.Age,
        Status: req.Status,
    }).Scan(&res.List)
    return
}

查询列表数据我们同样使用到了Scan方法,这个方法是非常强大的。同查询单条数据的逻辑一样,它仅会在查询的数据时才会初始化这里的&res.List。

学习小结

本章节的示例源码:https://github.com/gogf/quick-demo/tree/main/internal/controller/user

可以看到,使用GoFrame数据库ORM组件可以非常快速、高效地完成接口开发工作。整个CRUD接口开发下来,开发者需要实现的业务逻辑仅需要几行代码😼。

开发效率的提升,除了归功于脚手架工具自动生成的dao和controller代码之外,强大的数据库ORM组件也是功不可没。可以看到,我们在对数据库表进行操作时,代码量非常简洁优雅,但在数据库ORM组件的内部设计中,涉及很多精细的设计、严格的代码测试、年复一年的功能迭代的沉淀结果。

接口逻辑开发完了,在下一步,我们需要做一些数据库配置和路由注册的操作,同样也是非常简便,一起看看吧。

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.