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

≈709 tokens on demand. Your agent reads this file only when SKILL.md points to it.

在本示例,我们尝试通过数据结构化的方式,解决上一章节的参数名称硬编码问题。

请求对象

我们定义一个请求的数据结构来接收客户端提交的参数信息:

type HelloReq struct {
    Name string // 名称
    Age  int    // 年龄
}

太棒了,看起来我们既可以对参数进行注释描述,也能确定参数的类型,不再需要参数名称硬编码。

示例代码

我们调整我们前面的Web Server程序如下:

package main

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

type HelloReq struct {
    Name string // 名称
    Age  int    // 年龄
}

func main() {
    s := g.Server()
    s.BindHandler("/", func(r *ghttp.Request) {
        var req HelloReq
        if err := r.Parse(&req); err != nil {
            r.Response.Write(err.Error())
            return
        }
        if req.Name == "" {
            r.Response.Write("name should not be empty")
            return
        }
        if req.Age <= 0 {
            r.Response.Write("invalid age value")
            return
        }
        r.Response.Writef(
            "Hello %s! Your Age is %d",
            req.Name,
            req.Age,
        )
    })
    s.SetPort(8000)
    s.Run()
}

在本示例中:

  • 我们通过r.Parse方法将请求参数映射到请求对象上,随后可以通过对象的方式来使用参数。 r.Parse方法支持自动解析客户端提交参数,并赋值到指定对象上。 内部有固定的名称映射逻辑,您将在开发手册的类型转换组件中详细了解到,这里不作过多介绍。
  • 同时,我们在本示例中增加了校验逻辑,Name及Age参数不能为空。

执行结果

运行后,我们访问 http://127.0.0.1:8000/?name=john&age=18 可以看到,页面输出结果符合预期。

我们尝试一下错误的参数请求 http://127.0.0.1:8000/ 可以看到,参数被按照预期校验,页面输出结果同样符合预期

学习小结

在本章节中,我们学会了通过结构化请求对象的方式来规避参数名称硬编码的问题, 也能很好地维护参数的名称、描述和类型定义。 同时,我们也对参数增加了必要的校验逻辑,以保障参数的完整性。

但在该示例代码中,同样有值得改进的地方:

  • 其中的r.Parse是属于业务无关的操作,理应当独立于业务逻辑之外处理。
  • 如果接口比较多,所有的接口中都要重复进行r.Parse操作,操作比较冗余。
  • 针对数据校验,如果接口的参数比较多,进行大量的if数据校验操作太过于繁琐。

是否有更好的方式来简化并解决这些问题呢?答案是肯定的,我们下一个章节解决这一问题。

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.