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项目脚手架接口开发Step4 - 生成controller代码.md

根据api生成代码

当api定义完成后,我们通过make ctrl命令(或者gf gen ctrl)生成控制器代码。

$ make ctrl
generated: /Users/john/Temp/demo/api/user/user.go
generated: /Users/john/Temp/demo/internal/controller/user/user.go
generated: /Users/john/Temp/demo/internal/controller/user/user_new.go
generated: /Users/john/Temp/demo/internal/controller/user/user_v1_create.go
generated: /Users/john/Temp/demo/internal/controller/user/user_v1_update.go
generated: /Users/john/Temp/demo/internal/controller/user/user_v1_delete.go
generated: /Users/john/Temp/demo/internal/controller/user/user_v1_get_one.go
generated: /Users/john/Temp/demo/internal/controller/user/user_v1_get_list.go
done!

生成的代码主要包含3类文件。

api接口抽象文件

定义了api interface,用于保证控制器实现的接口完整性,避免controller缺失部分接口实现的问题。由于GoFrame是一款严谨的开发框架,这种细节控制得比较好,至于该特性开发者使用与否,可以根据自身场景和需要来选择。

/Users/john/Temp/demo/api/user/user.go

该文件由开发工具维护,开发者无需关心。

内容如下:

// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================

package user

import (
    "context"

    "demo/api/user/v1"
)

type IUserV1 interface {
    Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error)
    Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error)
    Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error)
    GetOne(ctx context.Context, req *v1.GetOneReq) (res *v1.GetOneRes, err error)
    GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error)
}

controller路由对象管理

用于管理控制器的初始化,以及一些控制内部使用的数据结构、常量定义。

generated: /Users/john/Temp/demo/internal/controller/user/user.go
generated: /Users/john/Temp/demo/internal/controller/user/user_new.go

其中internal/controller/user/user.go是一个空的源码文件,可用于定义一些控制器内部使用的数据结构、常量等内容。

// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================

package user

另一个internal/controller/user/user_new.go文件是自动生成的路由对象创建文件。

// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================

package user

import (
    "demo/api/user"
)

type ControllerV1 struct{}

func NewV1() user.IUserV1 {
    return &ControllerV1{}
}

这两个文件都只会生成一次,随后开发者可以随意修改、扩展。

:::tip 如果后续我们需要定义v2接口,make ctrl命令会类似生成type ControllerV2 struct{}结构体定义,以及func NewV2() user.IUserV2初始化方法。 :::

controller路由实现代码

用于具体的api接口实现的代码文件。默认情况下,会按照一个api接口一个源码文件的形式生成代码。当然,也可以控制按照api文件定义的接口聚合生成到对应的一个源码文件中。具体的命令介绍及配置请参考章节 接口规范-gen ctrl。

generated: /Users/john/Temp/demo/internal/controller/user/user_v1_create.go
generated: /Users/john/Temp/demo/internal/controller/user/user_v1_update.go
generated: /Users/john/Temp/demo/internal/controller/user/user_v1_delete.go
generated: /Users/john/Temp/demo/internal/controller/user/user_v1_get_one.go
generated: /Users/john/Temp/demo/internal/controller/user/user_v1_get_list.go

我们打开一个文件查看生成的代码模板:

package user

import (
    "context"

    "github.com/gogf/gf/v2/errors/gcode"
    "github.com/gogf/gf/v2/errors/gerror"

    "demo/api/user/v1"
)

func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
    return nil, gerror.NewCode(gcode.CodeNotImplemented)
}

可以看到,这只是我们定义的创建接口的实现模板,我们完善这个路由函数的具体业务逻辑即可。

学习小结

本章节的示例源码:https://github.com/gogf/quick-demo/tree/main/internal/controller/user

GoFrame脚手架工具,帮助我们将一切与业务逻辑无关的代码都生成好了,我们只需要关注业务逻辑实现即可。并且,这些自动生成的代码文件,除了开发者可以扩展的个别代码文件,大部分代码文件都完全由工具自动维护,我们也无需关心其未来的维护。

是的,GoFrame脚手架工具的目标,就是让开发者可以将精力聚焦于业务逻辑本身,而业务逻辑以外的工作,都交给开发框架和脚手架工具来完成。

这样的开发方式简直太方便了,不要太舒爽!你以为这就是全部了吗?当然不是,在快速开始章节我们只会介绍部分入门级功能。当你深入地接触她,你会发现她更多的好、她的善解人意。

下一步,我们将完成接口的业务逻辑实现,感受下GoFrame数据库ORM组件的魅力。

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.