Agent skill

go-dev

Go 开发规范,包含命名约定、错误处理、并发编程、测试规范等

Stars 424
Forks 44

Install this agent skill to your Project

npx add-skill https://github.com/doccker/cc-use-exp/tree/main/.claude/skills/go-dev

SKILL.md

Go 开发规范

参考来源: Effective Go、Go Code Review Comments、uber-go/guide


工具链

bash
goimports -w .                    # 格式化并整理 import
go vet ./...                      # 静态分析
golangci-lint run                 # 综合检查
go test -v -race -cover ./...     # 测试(含竞态检测和覆盖率)

命名约定

类型 规则 示例
包名 小写单词,不用下划线 user, orderservice
变量/函数 驼峰命名,缩写词一致大小写 userID, HTTPServer
常量 导出用驼峰,私有可驼峰或全大写 MaxRetryCount
接口 单方法用方法名+er Reader, Writer

禁止: common, util, base 等无意义包名


import 顺序

go
import (
    "context"           // 标准库
    "fmt"

    "github.com/gin-gonic/gin"  // 第三方库

    "project/internal/model"     // 项目内部
)

错误处理

必须处理错误,不能忽略:

go
// ✅ 好:添加上下文
if err != nil {
    return fmt.Errorf("failed to query user %d: %w", userID, err)
}

// ❌ 差:忽略错误
result, _ := doSomething()

错误包装: 使用 %w 保留错误链,用 errors.Is() / errors.As() 检查


并发编程

基本原则:

  • 优先使用 channel 通信
  • 启动 goroutine 前考虑:谁来等待它?怎么停止它?
  • 使用 context.Context 控制生命周期
go
// ✅ 好:使用 context 控制
func process(ctx context.Context) error {
    done := make(chan error, 1)
    go func() { done <- doWork() }()

    select {
    case err := <-done:
        return err
    case <-ctx.Done():
        return ctx.Err()
    }
}

数据竞争: 使用 go test -race 检测


测试规范

go
// 表驱动测试
func TestAdd(t *testing.T) {
    tests := []struct {
        name     string
        a, b     int
        expected int
    }{
        {"positive", 1, 2, 3},
        {"zero", 0, 0, 0},
    }

    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got := Add(tt.a, tt.b)
            if got != tt.expected {
                t.Errorf("Add(%d, %d) = %d, want %d", tt.a, tt.b, got, tt.expected)
            }
        })
    }
}

性能优化

陷阱 解决方案
循环中拼接字符串 使用 strings.Builder
未预分配 slice make([]T, 0, cap)
N+1 查询 批量查询 + 预加载
无限制并发 使用 semaphore 或 worker pool
Raw SQL 别名用了保留字 避免 year_month/order/status/rank 等 MySQL 保留字做别名
bash
# 性能分析
go test -cpuprofile=cpu.prof -bench=.
go tool pprof cpu.prof

项目结构

project/
├── cmd/                    # 可执行文件入口
├── internal/               # 私有代码
│   ├── handler/
│   ├── service/
│   ├── repository/
│   └── model/
├── pkg/                    # 公共代码
├── go.mod
└── go.sum

详细参考

文件 内容
references/go-style.md 命名约定、错误处理、并发、测试、性能
references/date-time.md 日期加减、账期计算、AddDate 溢出处理

📋 本回复遵循:go-dev - [具体章节]

Expand your agent's capabilities with these related and highly-rated skills.

doccker/cc-use-exp

ops-safety

运维安全规范。当用户执行系统级命令(sysctl、iptables、systemctl、Docker 配置、数据库 DDL) 或进行服务器运维操作时触发。 包含命令风险说明模板、回滚方案要求、问题排查原则、Docker/Cloudflare/数据库场景规则等。

424 44
Explore
doccker/cc-use-exp

ruanzhu

当用户执行 /ruanzhu 命令或请求生成软著源代码文档时触发。提供软著源代码 DOCX 生成规范。 覆盖项目信息检测、语言扫描规则、页数控制、DOCX 格式规范等。

424 44
Explore
doccker/cc-use-exp

ui-ux-pro-max

专业级 UI/UX 设计规范,需要高质量界面设计时手动触发或描述"设计感/专业UI"时自动触发。 覆盖视觉层次、配色体系、排版节奏、交互微动效、响应式适配等。 日常前端开发由 frontend-dev skill 覆盖。

424 44
Explore
doccker/cc-use-exp

bash-style

Bash 编写规范。当用户操作 .sh、Dockerfile、Makefile、.yml、.yaml 文件, 或在 Markdown 中编写 bash/shell 代码块时触发。 包含注释规范、文件写入方式、Heredoc 引号规则、权限路径、脚本规范等。

424 44
Explore
doccker/cc-use-exp

redis-safety

Redis 安全与性能规范。当用户操作 Redis 相关代码(go-redis、Jedis、redis-py、ioredis)时触发。 包含禁止 KEYS 命令、SCAN 替代、大 key 控制、Pipeline 批量、TTL 规范等。

424 44
Explore
doccker/cc-use-exp

python-dev

Python 开发规范。当用户操作 .py、pyproject.toml、requirements.txt、setup.py 文件, 或涉及 FastAPI、Django、Flask、pytest、asyncio 开发时触发。 包含 PEP 8 风格、类型注解、异常处理、测试规范、异步编程、性能优化等。

424 44
Explore

Didn't find tool you were looking for?

Be as detailed as possible for better results