npx skills add https://github.com/gogf/skills --skill goframe-v2gf init 创建项目脚手架。详情请参阅 项目创建 - init。logic/ 目录处理业务逻辑。业务逻辑应直接在 service/ 目录中实现。在创建新方法或变量之前,请检查其他地方是否已存在,并复用现有实现。
所有错误处理必须使用 gerror 组件,以确保完整的堆栈跟踪,便于追溯。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
探索新组件时,优先使用 GoFrame 内置组件,并参考示例中的最佳实践代码。
数据库操作必须使用 DO 对象(internal/model/do/),严禁使用 g.Map 或 map[string]interface{}。DO 结构体字段类型为 interface{};未设置的字段保持 nil 值,ORM 会自动忽略:
// 正确 - 使用 DO 对象
dao.Users.Ctx(ctx).Where(cols.Id, id).Data(do.User{Uid: uid}).Update()
// 正确 - 条件字段,未设置的字段为 nil 并被忽略
data := do.User{}
if password != "" { data.PasswordHash = hash }
if isAdmin != nil { data.IsAdmin = *isAdmin }
dao.Users.Ctx(ctx).Where(cols.Id, id).Data(data).Update()
// 正确 - 使用 gdb.Raw 显式将列设置为 NULL
dao.Instances.Ctx(ctx).Where(cols.Id, id).Data(do.Instance{IdleSince: gdb.Raw("NULL")}).Update()
// 错误 - 切勿使用 g.Map 进行数据库操作
dao.Users.Ctx(ctx).Data(g.Map{cols.Uid: uid}).Update()
变量声明:当定义多个变量时,使用 var 代码块将它们分组,以获得更好的对齐和可读性:
// 良好 - 对齐且清晰
var (
authSvc *auth.Service
bizCtxSvc *bizctx.Service
k8sSvc *svcK8s.Service
notebookSvc *notebook.Service
middlewareSvc *middleware.Service
)
// 避免 - 分散的声明
authSvc := auth.New()
bizCtxSvc := bizctx.New()
k8sSvc := svcK8s.New()
当在同一作用域内有 3 个或更多相关变量声明时,请应用此模式。
完整的 GoFrame 开发资源,涵盖组件设计、用法、最佳实践和注意事项:GoFrame 文档
丰富的实践代码示例,涵盖 HTTP 服务、gRPC 服务及各类项目类型:GoFrame 示例
每周安装量
750
代码仓库
GitHub 星标数
42
首次出现
2026年2月10日
安全审计
安装于
opencode738
codex737
gemini-cli733
github-copilot733
kimi-cli732
amp732
gf init to create project scaffolding. See Project Creation - init for details.logic/ directory for business logic. Implement business logic directly in the service/ directory.Before creating new methods or variables, check if they already exist elsewhere and reuse existing implementations.
Use the gerror component for all error handling to ensure complete stack traces for traceability.
When exploring new components, prioritize GoFrame built-in components and reference best practice code from examples.
Database Operations MUST use DO objects (internal/model/do/), never g.Map or map[string]interface{}. DO struct fields are interface{}; unset fields remain nil and are automatically ignored by the ORM:
// Good - use DO object
dao.Users.Ctx(ctx).Where(cols.Id, id).Data(do.User{Uid: uid}).Update()
// Good - conditional fields, unset fields are nil and ignored
data := do.User{}
if password != "" { data.PasswordHash = hash }
if isAdmin != nil { data.IsAdmin = *isAdmin }
dao.Users.Ctx(ctx).Where(cols.Id, id).Data(data).Update()
// Good - explicitly set a column to NULL using gdb.Raw
dao.Instances.Ctx(ctx).Where(cols.Id, id).Data(do.Instance{IdleSince: gdb.Raw("NULL")}).Update()
// Bad - never use g.Map for database operations
dao.Users.Ctx(ctx).Data(g.Map{cols.Uid: uid}).Update()
Variable Declarations : When defining multiple variables, use a var block to group them for better alignment and readability:
// Good - aligned and clean
var (
authSvc *auth.Service
bizCtxSvc *bizctx.Service
k8sSvc *svcK8s.Service
notebookSvc *notebook.Service
middlewareSvc *middleware.Service
)
// Avoid - scattered declarations
authSvc := auth.New()
bizCtxSvc := bizctx.New()
k8sSvc := svcK8s.New()
Apply this pattern when you have 3 or more related variable declarations in the same scope.
Complete GoFrame development resources covering component design, usage, best practices, and considerations: GoFrame Documentation
Rich practical code examples covering HTTP services, gRPC services, and various project types: GoFrame Examples
Weekly Installs
750
Repository
GitHub Stars
42
First Seen
Feb 10, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode738
codex737
gemini-cli733
github-copilot733
kimi-cli732
amp732
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
136,300 周安装