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.

referencesWEB服务开发请求输入请求输入-默认值绑定.md

从 v1.15 版本开始, Request 请求对象支持通过 struct tag 的方式为输入对象的属性绑定默认值。默认值的 struct tag 名称为 d(也可以使用 default)。

我们来看一个示例以便更好理解。

参数对象定义

type GetListReq struct {
    g.Meta `path:"/" method:"get"`
    Type   string `v:"required#请选择内容模型" dc:"内容模型"`
    Page   int    `v:"min:0#分页号码错误"      dc:"分页号码" d:"1"`
    Size   int    `v:"max:50#分页数量最大50条" dc:"分页数量,最大50" d:"10"`
    Sort   int    `v:"in:0,1,2#排序类型不合法" dc:"排序类型(0:最新, 默认。1:活跃, 2:热度)"`
}
type GetListRes struct {
    Items []Item `dc:"内容列表"`
}

type Item struct {
    Id    int64  `dc:"内容ID"`
    Title string `dc:"内容标题"`
}

这个是一个查询内容列表请求的参数接受对象,其中我们通过 d 的标签为属性 Page 和 Size 指定了默认值,当这两个参数不传递时,默认为 1 和 10,表示分页从第 1 页开始,每页查询数量为 10。

参数对象使用

package main

import (
    "context"

    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/ghttp"
)

type GetListReq struct {
    g.Meta `path:"/" method:"get"`
    Type   string `v:"required#请选择内容模型" dc:"内容模型"`
    Page   int    `v:"min:0#分页号码错误"      dc:"分页号码" d:"1"`
    Size   int    `v:"max:50#分页数量最大50条" dc:"分页数量,最大50" d:"10"`
    Sort   int    `v:"in:0,1,2#排序类型不合法" dc:"排序类型(0:最新, 默认。1:活跃, 2:热度)"`
}
type GetListRes struct {
    Items []Item `dc:"内容列表"`
}

type Item struct {
    Id    int64  `dc:"内容ID"`
    Title string `dc:"内容标题"`
}

type Controller struct{}

func (Controller) GetList(ctx context.Context, req *GetListReq) (res *GetListRes, err error) {
    g.Log().Info(ctx, req)
    return
}

func main() {
    s := g.Server()
    s.Group("/content", func(group *ghttp.RouterGroup) {
        group.Middleware(ghttp.MiddlewareHandlerResponse)
        group.Bind(&Controller{})
    })
    s.SetPort(8199)
    s.Run()
}

我们访问以下地址,并查看服务端终端输出结果:

http://127.0.0.1:8199/content?type=ask

2023-03-21 21:58:23.058 [INFO] {2883f9c2dc734e170a35c73ea3560b4b} {"Type":"ask","Page":1,"Size":10,"Sort":0}

http://127.0.0.1:8199/content?type=ask&page=

2023-03-21 21:58:32.555 [INFO] {b86e22f9de734e170b35c73edf07859d} {"Type":"ask","Page":1,"Size":10,"Sort":0}

http://127.0.0.1:8199/content?type=ask&page=2

2023-03-21 22:01:02.907 [INFO] {a016c8fa01744e170f35c73e99082f53} {"Type":"ask","Page":2,"Size":10,"Sort":0}

可以看到,当调用端不传递或者传递空的 page 参数时,服务端都将使用定义的默认值;而当调用端传递具体的 page 参数时,默认值并不会生效。

注意事项

默认值参数绑定是根据客户端 未提交该参数 来识别是否启用默认值,如果客户端已经提交了该参数, 即便该参数值是空字符串 也会被当做客户端已经传递了具体的值,那么服务端数据结构上的默认值标签将不会生效。

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.