Golang pro

From findscripter/everything-skills · View interactive version

Golang pro is an AI agent skill from the GitHub repository findscripter/everything-skills. 当用 Go 1.21+ 构建服务/CLI/微服务、设计并发模型或排查竞态与性能时使用;产出符合 Go 惯例的并发架构、可测代码与性能/可观测性方案(pprof、slog、表驱动测试);不适用于其他语言运行时、仅需基础语法解释、或无法改动工具链与构建配置的场景;触发词:goroutine、worker pool、pprof、gRPC、race condition

How to use this skill

Skill file

何时使用

适用:

不该用(负边界):

步骤

  1. 对齐前提:确认 Go 版本、工具链、运行时与部署约束(容器、K8s、内存上限)。
  2. 选型:依据负载(CPU-bound vs I/O-bound)选并发模式与架构(清晰/六边形架构、组合优先于继承)。
  3. 实现:接口做抽象,显式错误处理与 %w 包装,配套表驱动测试与 benchmark。
  4. 优化:先测量后优化——用 pprof / trace 定位热点,再调 GC、连接池、内存池。

指令

示例

带优雅关闭的 worker pool 骨架(context 取消 + WaitGroup):

func runPool(ctx context.Context, jobs <-chan Job, n int) {
    var wg sync.WaitGroup
    for i := 0; i < n; i++ {
        wg.Add(1)
        go func() {
            defer wg.Done()
            for {
                select {
                case <-ctx.Done():           // 取消信号,退出避免泄漏
                    return
                case job, ok := <-jobs:
                    if !ok {
                        return               // 通道关闭,正常收尾
                    }
                    if err := job.Do(ctx); err != nil {
                        slog.Error("job failed", "id", job.ID, "err", err)
                    }
                }
            }
        }()
    }
    wg.Wait()
}

典型请求:

注意事项

互见


采编自 sickn33/antigravity-awesome-skills(MIT)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.