Skip to content
Development
Skill

/go-dev

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

From plugin
cc-use-exp
1.1k24 skills18 commands

Claude Code

$ npx -y skills add doccker/cc-use-exp --skill go-dev --agent claude-code

SKILL.md

go-dev.SKILL.md
name: go-dev
description: Go 开发规范,包含命名约定、错误处理、并发编程、测试规范等
version: v3.0
paths:
  - "**/*.go"
  - "**/go.mod"
  - "**/go.sum"

Go 开发规范

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

---

工具链

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 顺序

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

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

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

---

错误处理

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

// ✅ 好:添加上下文
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` 控制生命周期
// ✅ 好:使用 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` 检测

---

测试规范

// 表驱动测试
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 保留字做别名 |

# 性能分析
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` - [具体章节]

Read more
@doccker
Ships withcc-use-exp

保留你熟悉的 CLI/IDE,让 Claude Code、Gemini CLI、Codex、Cursor、GitHub Copilot 开箱即用 按费力度从低到高,用最少操作获得最大帮助 不是提示词集合,而是一套可维护的 AI 协作配置系统。

Stars on GitHub

Momentum

stars in total
1.1k
Star history of doccker/cc-use-exp on GitHub, a chart by star-history.com

Other skills on cc-use-exp.

api-design-safety

api-design-safety

当设计或修改 REST API 响应结构、处理 API 返回值时触发。防止 API 设计缺陷导致的字段错位、类型歧义等问题。

@doccker@docckerInstall Skill
api-proxy-safety

api-proxy-safety

网关/代理/WAF/CDN 中间件的安全关键词匹配实现规范,防止纯子串匹配误判正常响应内容中的技术术语(如 Cloudflare、502、error)

@doccker@docckerInstall Skill
async-task-pattern

async-task-pattern

当 API/任务可能执行超过 10 秒(批量数据处理、远程 API 批量调用、全表扫描、跨租户聚合)时触发。防止同步接口被网关 30s 超时切断、用户重复点击触发并发、状态缓存内存泄漏等问题。提供异步任务状态机标准模板。

@doccker@docckerInstall Skill
bash-style

bash-style

当用户操作 .sh、Dockerfile、Makefile、.yml、.yaml 文件,或在 Markdown 中编写 bash 代码块时触发。提供 Bash 编写规范。

@doccker@docckerInstall Skill
code-quality-principles

code-quality-principle…

当编写新模块、设计接口、重构代码或代码审查时触发。提供经典模块化六原则检查清单(大小适中/调用深度/扇入扇出/边界清晰/作用域内聚/可预测性),适用于 PR/Review/新模块设计场景。

@doccker@docckerInstall Skill
external-system-debugging

external-system-debugg…

涉及浏览器、编辑器、CDN/WAF、IM 平台、操作系统剪贴板、第三方 SaaS 等"外部黑盒系统"的代码编写或 bug 调试时触发。强制先抓真实环境数据再推理,避免连续 2 轮"凭代码推理"的修复…

@doccker@docckerInstall Skill

More skills from other plugins.

brainstorming
Auto-invoked

brainstorming

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or…

@obra@obraInstall Skill
ask-matt

ask-matt

Ask which skill or flow fits your situation. A router over the skills in this repo.

skills10d309k
@mattpocock@mattpocockInstall Skill
accessibility
Auto-invoked

accessibility

Design, implement, and audit accessible UI to WCAG 2.2 Level AA across Web, iOS, and Android — semantic ARIA roles and labels,…

ecc12d293k
@affaan-m@affaan-mInstall Skill
compiler-commit

compiler-commit

Use when you want to verify compiler changes and commit with the correct convention. Runs tests, lint, and format, then commits…

react1d252k
@facebook@facebookInstall Skill

Questions about go-dev.

Which coding agents can use go-dev?
go-dev installs in Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode and Windsurf with the skills CLI, using each agent's flag shown on this page. Flowy's auto-invocation, which fires a skill when your prompt matches, runs in Claude Code only.