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.8k tokens on demand. Your agent reads this file only when SKILL.md points to it.

从本章节开始,我们主要主要以完整的业务项目为主,介绍如何使用框架提供的脚手架完成一个简单的接口项目。

:::warning 在进一步开始之前请您注意,由于 GoFrame 算得上真正意义的 Golang 企业级、工程化的开发框架,她设计严谨、易于使用、文档丰富且社区活跃, 一旦您学习并掌握了框架的使用,您将难以回到过去刀耕火种的开发年代。

如果您在快速开始的任何章节中遇到任何问题,欢迎在评论区留言💬,我们将尽可能及时为您提供解答🌟🌟。 :::

安装框架工具

框架提供的开发工具为开发者提供了便捷的开发指令简化开发工作,提供了例如工程脚手架、代码自动生成、工具及框架更新等实用命令。

方式一:源码安装

通过下载源码到本地编译生成二进制后安装到系统目录,使用的是go install命令,这是万能的安装方式:

go install github.com/gogf/gf/cmd/gf/v2@latest

方式二:预编译安装

预编译安装是开源社区预编译好的二进制文件,供开发者直接使用,二进制文件托管存放到 github 仓库中。工具下载地址: https://github.com/gogf/gf/releases

Mac 环境
wget -O gf "https://github.com/gogf/gf/releases/latest/download/gf_$(go env GOOS)_$(go env GOARCH)" && chmod +x gf && ./gf install -y && rm ./gf

您可能会遇到以下常见问题:

  • 如果当前系统没有安装 wget 命令,那么请使用 brew install wget 安装后继续执行该命令。
  • 🔥如果您使用的是 zsh 终端,可能会存在 gf 别名冲突( git fetch 快捷指令),那么安装后(至少执行一次)请 重启终端软件 来继续使用。
  • 您的Golang 开发环境未准备好,缺少GOOS或GOARCH环境变量,建议您先查阅环境安装文档:环境安装。
  • 若无法解决的其他问题,那么可以使用后续的【源码编译安装】方式。
Linux 环境
wget -O gf "https://github.com/gogf/gf/releases/latest/download/gf_$(go env GOOS)_$(go env GOARCH)" && chmod +x gf && ./gf install -y && rm ./gf

如果系统没有安装 wget 命令:

  • Ubuntu/Debian 系统请使用 apt-get install wget -y
  • CentOS/RedHat 系统请使用 yum install wget -y
Windows 环境

手动下载对应的二进制文件,双击按照终端指令安装即可。如果双击安装失败,请采用最后万能的【源码编译安装】方式。

验证安装

执行 gf -v 指令如果能打印出例如以下信息,表示您已成功安装好了框架脚手架工具 👍

$ gf -v
v2.7.4
Welcome to GoFrame!
Env Detail:
  Go Version: go1.23.1 darwin/arm64
  GF Version(go.mod): cannot find go.mod
CLI Detail:
  Installed At: /Users/john/go/bin/gf
  Built Go Version: go1.23.1
  Built GF Version: v2.7.4
Others Detail:
  Docs: https://goframe.org
  Now : 2024-10-29T13:30:30+08:00

注意其中的 Go/GF Version 是当前编译的二进制文件使用的 Golang 及 GoFrame 框架版本,其中的 GoFrame Version 是当前项目使用的 GoFrame 框架版本(自动检测当前目录下的 go.mod)。

:::info 常见问题注意事项:🔥如果您使用的是 zsh 终端,可能会存在 gf 别名冲突( git fetch 快捷指令),那么安装后(至少执行一次)请 重启终端软件 来继续使用。 :::

创建项目模板

gf init demo -u

该命令创建一个工程脚手架目录,项目名称是 demo,其中的 -u 参数用户指定是否更新项目中使用的 goframe 框架为最新版本。 GoFrame框架有独特的项目工程结构,工程目录结构介绍具体请参考: 工程目录设计。

框架的脚手架目录是按照通用性设计的,可以满足WEB、终端、微服务等业务开发场景。默认会生成一个 HTTP Web Server 的模板项目。在理解完成目录含义后,如果其中有不需要的目录,可以自行删除。

运行项目模板

项目模板可以执行以下命令运行:

cd demo && gf run main.go

其中的 gf run 是框架开发工具的动态编译命令,也可以替换为 go run 命令。

执行后,终端输出:

$ cd demo && gf run main.go
build: main.go
go build -o ./main  main.go
./main
build running pid: 76159
2022-08-22 12:20:59.058 [INFO] swagger ui is serving at address: http://127.0.0.1:8000/swagger/
2022-08-22 12:20:59.058 [INFO] openapi specification is serving at address: http://127.0.0.1:8000/api.json
2022-08-22 12:20:59.059 [INFO] pid[76159]: http server started listening on [:8000]

  ADDRESS | METHOD |   ROUTE    |                             HANDLER                             |           MIDDLEWARE
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | ALL    | /*         | github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing | GLOBAL MIDDLEWARE
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | ALL    | /api.json  | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec           |
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | GET    | /hello     | demo/internal/controller.(*cHello).Hello                        | ghttp.MiddlewareHandlerResponse
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | ALL    | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI             | HOOK_BEFORE_SERVE
----------|--------|------------|-----------------------------------------------------------------|----------------------------------

默认情况下项目会设置Web服务端口为 8000、开启 OpenAPI 接口文档、展示 Swagger 接口文档页面,这些关键信息都会展示到终端。默认情况下,会打印所有的路由信息到终端,该项目模板只会添加一个仅供演示的路由: /hello。

默认路由: http://127.0.0.1:8000/hello

Swagger 接口文档页面:

升级框架版本

随时可以在项目根目录下(目录下有 go.mod),执行以下命令更新使用最新的框架版本:

gf up -a

学习小结

通过本章节的学习,我们知道如何下载和安装脚手架的cli工具,并通过该工具创建一个脚手架项目模板。

在下一章节,我们简单介绍一下这个脚手架项目模板的项目启动流程。

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.