Skip to content
FunCoding

Search

Search docs, Skills and MCP

go-dev

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

项目与协作1.1k.antigravity/skills/go-dev/SKILL.md

Install

Send this to Claude Code, Codex or Cursor. The agent checks the Skill for safety first and installs it only after you confirm.

读取 https://funcoding.ai/skills/doccker/cc-use-exp/antigravity-skills-go-dev/install.md ,按里面的步骤帮我安装这个 Skill。

SKILL.md

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
接口单方法用方法名+erReader, 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
未预分配 slicemake([]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 - [具体章节]

Similar Skills

slack-gif-creator
anthropics/skills180k

slack-gif-creator

Knowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."

Projects & collaboration

observability-and-instrumentation
addyosmani/agent-skills103k

observability-and-instrumentation

Instruments code so production behavior is visible and diagnosable. Use when adding logging, metrics, tracing, or alerting. Use when shipping any feature that runs in production and you need evidence it works. Use when production issues are reported but you can't tell what happened from the available data.

Projects & collaboration

understand-diff
Egonex-AI/Understand-Anything86k

understand-diff

Use when you need to analyze git diffs or pull requests to understand what changed, affected components, and risks

Projects & collaboration

skill-share
ComposioHQ/awesome-claude-skills77k

skill-share

A skill that creates new Claude skills and automatically shares them on Slack using Rube for seamless team collaboration and skill discovery.

Projects & collaboration

slack-gif-creator
ComposioHQ/awesome-claude-skills77k

slack-gif-creator

Toolkit for creating animated GIFs optimized for Slack, with validators for size constraints and composable animation primitives. This skill applies when users request animated GIFs or emoji animations for Slack from descriptions like "make me a GIF for Slack of X doing Y".

Projects & collaboration

connect-apps
ComposioHQ/awesome-claude-skills77k

connect-apps

Connect Claude to external apps like Gmail, Slack, GitHub. Use this skill when the user wants to send emails, create issues, post messages, or take actions in external services.

Projects & collaboration