KeepRule

HE
henuwangkai
Community
👁 5·💰 ~0.01 USD·📅 Published Mar 24, 2026·📖 1 min read

Go-Zero

1. KeepRule 后端开发 Skill (Go-Zero)

Go-Zero 微服务后端开发 Skill,含 API 定义、代码生成、业务逻辑规范

Prompt

---
name: Go-Zero 后端规范
description: Go-Zero 后端开发规范。API 定义在 apilist/*.api、goctl api go 生成命令、目录结构(handler/logic/model)、响应格式(code/message/data)。禁止直接修改 types.go。
---

# 后端开发规范 (Go-Zero)

## 核心规则

- API 前缀统一为 `/api/v1/buffett/xxx`
-  API 文件为 `apilist/api.api`,模块 API  `apilist/buffett/` 子目录
- 新增/修改 API 必须先修改 `.api` 文件,再执行 `goctl api go` 生成代码
- **禁止直接修改 `internal/types/types.go`**

### 数据库操作封装规范

- 所有数据库查询、增加、删除(软删除)等**原子操作**,必须在 `model/` 目录下对应的 `*_model.go`(非 `*_gen.go`)文件中实现
- **Logic 层应保持纯净**,只负责业务逻辑组装,通过调用 Model 层提供的方法来操作数据
- **绝对禁止**在 Logic 层直接写 SQL 查询

## goctl 安装

```bash
go install github.com/zeromicro/go-zero/tools/goctl@latest

API 代码生成

cd backend
goctl api go -api ./apilist/api.api -dir . -style=go_zero --home ./dev/template/

Model 代码生成

cd backend
./goctl-model.sh <group_name> <table_name>

目录结构

keeprule/backend/
├── api.go                          # 主入口
├── apilist/                        # API 定义
│   ├── api.api                     # 主入口(import 各模块)
│   └── buffett/                    # 模块 API
│       ├── categories/categories.api
│       ├── rules/rules.api
│       └── chat/chat.api
│
├── internal/
│   ├── config/config.go            # 配置结构
│   ├── handler/                    # HTTP 处理器(自动生成)
│   ├── logic/                      # 业务逻辑(手写)
│   ├── svc/serviceContext.go       # 服务上下文
│   └── types/types.go              # 类型定义(自动生成,禁止手改)
│
├── model/                          # 数据库模型
│   ├── rules/                      # 规则相关
│   ├── user/                       # 用户相关
│   └── model/                      # AI 模型相关
│
├── dev/template/                   # goctl 模板
├── etc/                            # 配置文件
│   ├── api.yaml                    # 生产配置
│   └── api.local.yaml              # 本地配置
├── storage/logs/                   # 日志目录
└── goctl-model.sh                  # Model 生成脚本

API 接口列表

接口 方法 说明
/api/v1/buffett/categories/list GET 获取大师列表
/api/v1/buffett/categories/detail GET 获取大师详情
/api/v1/buffett/rules/list GET 获取原则列表
/api/v1/buffett/rules/detail GET 获取原则详情
/api/v1/buffett/chat/send POST AI 对话
/api/v1/buffett/chat/history GET 获取聊天历史

API 响应格式

统一响应结构

{
    "code": 200,
    "message": "操作成功",
    "data": { ... }
}

响应码说明

响应码 含义
200 成功
400 参数错误
401 未授权
500 服务器错误

响应方法(Go 代码)

// 成功响应
response.Success(w, data)
response.SuccessWithMessage(w, data, "自定义消息")

// 错误响应
response.Error(w, 401, "用户未登录")
response.Error(w, 500, "服务器内部错误")

启动命令

cd backend
go run . -f etc/api.local.yaml
# 服务运行在 http://localhost:8001

Discussion

Discussion

Sign in to join the discussion.
MC
Maya Chen·2 hours ago

Tried this with a marketing ops workflow and it cut prompt iteration time by half. The Prompt section is especially reusable.

LW
Leo Wang·Yesterday

Would love a follow-up showing how you adapted this for team use.

  • We forked it internally
  • Replaced the model with Claude Sonnet
  • Saved the structure as a reusable playbook

Related Assets

Related Assets

Other assets published by the same creator.

Back to home