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项目脚手架接口开发Step2 - 生成dao_do_entity.md

检查工具配置

在使用脚手架工具之前,请检查本地的cli工具配置是否正确。默认的配置如下:


# CLI tool, only in development environment.
# https://goframe.org/docs/cli
gfcli:
  gen:
    dao:
    - link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
      descriptionTag: true

  docker:
    build: "-a amd64 -s linux -p temp -ew"
    tagPrefixes:
    - my.image.pub/my-app
  • 其中的dao部分配置即本次将要执行的命令的配置,其中的link为需要连接的数据库配置。descriptionTag表示为生成的entity代码文件增加字段描述到description标签中,如果数据表的entity对象被用到了接口api定义中,该标签则可作为参数描述。我们需要将此link配置改为我们的数据库连接地址。关于link配置项的详细介绍请参考章节 ORM使用配置-配置文件。
  • 其中docker配置项是模板默认提供的配置,用于镜像编译。这里不做详解,感兴趣可以参考开发手册中开发工具相关章节。

执行代码生成

将数据表创建好后,我们在项目根目录下执行make dao自动生成对应的dao/do/entity文件。

$ make dao
generated: /Users/john/Temp/demo/internal/dao/user.go
generated: /Users/john/Temp/demo/internal/dao/internal/user.go
generated: /Users/john/Temp/demo/internal/model/do/user.go
generated: /Users/john/Temp/demo/internal/model/entity/user.go
done!

:::tip make命令是在*nix系统下默认提供的指令,包括Linux/MacOS系统。如果是其他系统,例如Windows,默认不支持make命令,可以使用gf gen dao命令来替换。 :::

每张表将会生成三类Go文件:

  • dao:通过对象方式访问底层数据源,底层基于ORM组件实现。
  • do:数据转换模型,用于业务模型到数据模型的转换,由工具维护,用户不能修改。工具每次生成代码文件将会覆盖该目录。
  • entity:数据模型,由工具维护,用户不能修改。工具每次生成代码文件将会覆盖该目录。

更详细的介绍请参考章节 数据规范-gen dao

:::info 由脚手架工具生成的代码中,都会带有顶部文件注释。如果文件注释中带有Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.的注释描述,那么表示该文件是由脚手架工具维护,每一次代码生成都会覆盖它。 :::

dao

生成的dao文件有两个:

  • internal/dao/internal/user.go用于封装对数据表user的访问。该文件自动生成了一些数据结构和方法,简化对数据表的CRUD操作。该文件每次生成都会覆盖,由开发工具自动维护,开发者无需关心。
  • internal/dao/user.go其实是对internal/dao/internal/user.go的进一步封装,用于供其他模块直接调用访问。该文件开发者可以随意修改,或者扩展dao的能力。

由于生成的internal/dao/internal/user.go文件完全由开发工具维护,那么我们只需关心internal/dao/user.go这个生成的源码文件,后续如果有需要可以在这个文件上做功能扩展。

示例源码:https://github.com/gogf/quick-demo/blob/main/internal/dao/user.go

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

package dao

import (
    "demo/internal/dao/internal"
)

// internalUserDao is internal type for wrapping internal DAO implements.
type internalUserDao = *internal.UserDao

// userDao is the data access object for table user.
// You can define custom methods on it to extend its functionality as you wish.
type userDao struct {
    internalUserDao
}

var (
    // User is globally public accessible object for table user operations.
    User = userDao{
        internal.NewUserDao(),
    }
)

// Fill with you ideas below.

do

示例源码:https://github.com/gogf/quick-demo/blob/main/internal/model/do/user.go

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

package do

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

// User is the golang structure of table user for DAO operations like Where/Data.
type User struct {
    g.Meta `orm:"table:user, do:true"`
    Id     interface{} // user id
    Name   interface{} // user name
    Status interface{} // user status
    Age    interface{} // user age
}

关于do数据转换模型的使用,我们将在后续代码逻辑中做展示。

entity

示例源码:https://github.com/gogf/quick-demo/blob/main/internal/model/entity/user.go

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

package entity

// User is the golang structure for table user.
type User struct {
    Id     uint   `json:"id"     orm:"id"     description:"user id"`     // user id
    Name   string `json:"name"   orm:"name"   description:"user name"`   // user name
    Status int    `json:"status" orm:"status" description:"user status"` // user status
    Age    uint   `json:"age"    orm:"age"    description:"user age"`    // user age
}

可以看到该entity数据结构定义与数据表字段一一对应。

学习小结

可以感受到,使用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.