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

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

当您开始第一个项目的时候,可能对工程下面有这么多目录感觉困惑,没关系,您可以通过这个章节 工程目录设计🔥 先了解一下各个目录的作用。接下来我们会介绍一下项目是如何启动的,一个程序的启动串联了哪些目录,让大家对程序整体启动的经脉有所了解。

main.go

所有的程序入口都是由 main.go 进入,该文件主要是调用 internal/cmd 包的对应命令引导程序启动。在项目模板中,默认会执行 internal/cmd 包的 Main 对象 Run 命令引导程序启动。

项目的所有的核心业务逻辑都是放到了internal目录下,该目录是Golang的特性,用于对外隐藏可见性。internal目录下面的代码内容将无法被外部通过import的方式引用,提高项目的安全性和简洁性。

:::tip 框架的核心组件均需要传递 context 上下文参数,这里使用 gctx.GetInitCtx 表示承接父进程的链路跟踪信息,如果没有父进程那么会创建一个带链路跟踪特性的 context 上下文对象给下游链路。 :::

引导启动

Main 对象的 Run 命令的主要作用是做引导启动,将一些动态初始化的逻辑放到 Main 的 Run 方法中。在项目模板中,默认创建一个 HTTP Server,然后通过分组路由的方式注册路由,并启动 HTTP Server。随后 HTTP Server 将会阻塞运行,它同时也会异步监听系统信号,直至收到退出信号后,它会优雅关闭连接随后退出进程。

:::tip 框架的命令行管理默认采用了结构化的对象管理方式,详细介绍感兴趣请参考章节:命令管理 :::

路由注册

在项目模板中使用了 Group 方法创建了分组路由,框架的 HTTP Server 支持多种路由注册方式,而分组路由也是最常见的路由注册方式。

s := g.Server()
s.Group("/", func(group *ghttp.RouterGroup) {
    group.Middleware(ghttp.MiddlewareHandlerResponse)
    group.Bind(
        hello.NewV1(),
    )
})
  • 在分组路由的闭包方法内部,通过 Middleware 方法注册了一个中间件,该中间件是 HTTP Server 组件用于规范化路由的数据返回。
  • 随后通过 Bind 方法的规范化路由方式绑定一个 hello.NewV1() 返回的路由对象,该路由对象下的所有公开方法均会被自动注册到路由。我们的项目脚手架支持接口的版本管理,默认情况下我们的路由对象都是v1版本,并且通过NewV1的方式创建。

:::tip 详细的HTTP Server路由介绍请参考章节: 路由管理🔥 :::

路由对象

对象创建

路由对象由hello.NewV1()方法返回,其定义如下:

可以看到NewV1方法其实返回的是一个接口,而不是具体的对象。该接口的定义如下:

为什么这里不直接返回ControllerV1对象,而是返回hello.IHelloV1接口呢?

试想一下,假如咱们定义了很多api接口,但是具体实现的controller只实现了其中一部分,而在运行时才能发现有缺失。那么我们如何能提前发现这个问题呢?对的,就是依赖这个接口实现。假如controller只实现了部分接口,那么在编译阶段就会展示错误,并且现代的编辑器中也会提前发现告警。

:::tip

  • 首先,并不是必需这么做,脚手架模板提供的是比较严谨的代码编写方式。

  • 其次,这里的绝大部分代码,是通过cli工具的gf gen ctrl命令自动解析api目录的接口定义自动生成的。 :::

函数定义

对应的路由函数定义如下:

Hello 方法对应的路由信息是定义到 HelloReq 输入参数对象中的,该对象的数据结构定义如下:

:::tip 这种通过统一的中间件返回统一的数据结构,统一路由对象的方法管理路由的方式,叫做规范路由。 更详细的介绍请参考章节:路由注册-规范路由 。 :::

运行服务

阻塞运行

通过 HTTP Server 的 Run 方法启动 HTTP Server,随后 HTTP Server 将会阻塞运行接收客户端请求,并监听进程信号,用于 HTTP Server 重启/关闭。

运行后,终端输出如下:

可以看到,我们启用了API接口文档以及Swagger UI。

查看效果

我们访问 http://127.0.0.1:8000/hello 查看接口效果:

我们访问 http://127.0.0.1:8000/swagger 查看Swagger UI:

学习小结

通过本章节的学习,我们了解了脚手架项目模板程序的执行流程。 这里其实有比较多的细节没有详细讲解,感兴趣可以了解下对应栏目下的关联文档资料。

在下一章节,我们将尝试着通过脚手架项目模板编写简单的CRUD接口,实现对数据库表的增删查改。

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.