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项目脚手架接口开发Step7 - 启动与测试.md

启动服务

我们可以使用命令行或者IDE自带的启动工具来启动服务,为了简化示例,我们这里直接在命令行中使用go run main.go来启动我们的服务:

$ go run main.go
2024-11-16 16:40:07.394 [INFO] pid[39511]: http server started listening on [:8000]
2024-11-16 16:40:07.394 [INFO] {18594fad2e66081870e88c6e1440060b} swagger ui is serving at address: http://127.0.0.1:8000/swagger/
2024-11-16 16:40:07.394 [INFO] {18594fad2e66081870e88c6e1440060b} openapi specification is serving at address: http://127.0.0.1:8000/api.json

  ADDRESS | METHOD |   ROUTE    |                        HANDLER                        |           MIDDLEWARE             
----------|--------|------------|-------------------------------------------------------|----------------------------------
  :8000   | ALL    | /api.json  | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec |                                  
----------|--------|------------|-------------------------------------------------------|----------------------------------
  :8000   | GET    | /hello     | demo/internal/controller/hello.(*ControllerV1).Hello  | ghttp.MiddlewareHandlerResponse  
----------|--------|------------|-------------------------------------------------------|----------------------------------
  :8000   | ALL    | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI   | HOOK_BEFORE_SERVE                
----------|--------|------------|-------------------------------------------------------|----------------------------------
  :8000   | GET    | /user      | demo/internal/controller/user.(*ControllerV1).GetList | ghttp.MiddlewareHandlerResponse  
----------|--------|------------|-------------------------------------------------------|----------------------------------
  :8000   | POST   | /user      | demo/internal/controller/user.(*ControllerV1).Create  | ghttp.MiddlewareHandlerResponse  
----------|--------|------------|-------------------------------------------------------|----------------------------------
  :8000   | DELETE | /user/{id} | demo/internal/controller/user.(*ControllerV1).Delete  | ghttp.MiddlewareHandlerResponse  
----------|--------|------------|-------------------------------------------------------|----------------------------------
  :8000   | GET    | /user/{id} | demo/internal/controller/user.(*ControllerV1).GetOne  | ghttp.MiddlewareHandlerResponse  
----------|--------|------------|-------------------------------------------------------|----------------------------------
  :8000   | PUT    | /user/{id} | demo/internal/controller/user.(*ControllerV1).Update  | ghttp.MiddlewareHandlerResponse  
----------|--------|------------|-------------------------------------------------------|----------------------------------

可以看到,我们开发的CRUD接口已经成功注册到Web Server上,并正确显示到了终端。同时,我们启用了接口文档特性,我们看看自动生成的接口文档。

接口文档

根据终端打印的地址,我们访问:http://127.0.0.1:8000/swagger/

:::tip 自动化的接口文档生成也是GoFrame框架提供的非常强大的特性之一,我们这里不做详细的介绍,感兴趣的朋友可以参考章节:接口文档 :::

接口测试

为了简化测试操作,我们使用curl命令来做测试,并使用json格式提交和接受返回参数。

:::tip 以下执行命令中前面的$符号,表示终端命令行工具的提示符号,并不是我们输入命令的一部分,不同的终端命令行提示符号不同。 :::

创建接口

创建请求需要使用POST方式提交。

$ curl -X POST 'http://127.0.0.1:8000/user' -d '{"name":"john","age":20}'
{"code":0,"message":"","data":{"id":1}}

$ curl -X POST 'http://127.0.0.1:8000/user' -d '{"name":"alice","age":18}'
{"code":0,"message":"","data":{"id":2}}

这里返回的code为0表示执行成功。

我们构造不符合校验规则的请求,看看效果:

$ curl -X POST 'http://127.0.0.1:8000/user' -d '{"name":"smith","age":16}'
{"code":51,"message":"The Age value `16` must be between 18 and 200","data":null}

$ curl -X POST 'http://127.0.0.1:8000/user' -d '{"name":"sm","age":18}'
{"code":51,"message":"The Name value `sm` length must be between 3 and 10","data":null}

可以看到,校验规则起了作用,返回了具体的校验错误信息,错误码code为51,这个是框架内置的错误码,表示校验错误,开发者也可以自定义错误码。更多的错误码介绍请查看开发手册相关章节。

查询接口

查询单条数据
$ curl -X GET 'http://127.0.0.1:8000/user/1'
{"code":0,"message":"","data":{"id":1,"name":"john","status":0,"age":20}}

$ curl -X GET 'http://127.0.0.1:8000/user/2'
{"code":0,"message":"","data":{"id":2,"name":"alice","status":0,"age":18}}
查询数据列表
$ curl -X GET 'http://127.0.0.1:8000/user'
{"code":0,"message":"","data":{"list":[{"id":1,"name":"john","status":0,"age":20},{"id":2,"name":"alice","status":0,"age":18}]}}

$ curl -X GET 'http://127.0.0.1:8000/user?age=18'
{"code":0,"message":"","data":{"list":[{"id":2,"name":"alice","status":0,"age":18}]}}

修改接口

创建请求需要使用PUT方式提交。

$ curl -X PUT 'http://127.0.0.1:8000/user/1' -d '{"age":26}'
{"code":0,"message":"","data":null}

执行成功后,我们再查询对应的数据,看看是否已经修改成功:

$ curl -X GET 'http://127.0.0.1:8000/user/1'
{"code":0,"message":"","data":{"id":1,"name":"john","status":0,"age":26}}

删除接口

创建请求需要使用DELETE方式提交。

$ curl -X DELETE 'http://127.0.0.1:8000/user/1'
{"code":0,"message":"","data":null}

执行成功后,我们再查询所有的数据列表,看看是否已经被删除:

$ curl -X GET 'http://127.0.0.1:8000/user'
{"code":0,"message":"","data":{"list":[{"id":2,"name":"alice","status":0,"age":18}]}}

可以看到,数据已经被成功删除了。

学习小结

可以看到,通过GoFrame框架的脚手架工具生成的项目模板,我们可以很高效、快速地完成接口开发工作,并且能自动生成接口文档,方便前后端协作。

到此为止,一个使用GoFrame框架的CRUD接口项目便快速完成了。但GoFrame框架的优秀之处,还远不止于此,她的更多特性,等待着您的进一步探索。

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.