Community编程与开发github.com

samber/golang-how-to

Go 技能编排器 — 在任何 Go 编码、审查、调试或设置任务中自动激活。读取任务上下文并加载 `samber/cc-skills-golang` 中的最相关技能,通常同时加载多个:编写 gRPC 服务时加载 golang-grpc + golang-testing + golang-error-handling;调试 panic 时加载 golang-troubleshooting + golang-safety;审计安全时加载 golang-security + golang-lint + golang-safety。同时,它还能消除冲突技能之间的歧义(性能 vs 基准测试 vs 故障排查,samber/lo vs mo vs ro,依赖注入集群,安全 vs 安全性),并配置项目的 agent-config 文件(CLAUDE.md、AGENTS.md、GEMINI.md、Cursor 规则或 Copilot 指令),以强制在项目中触发技能(/golang-how-to configure)。

golang-how-to 是什么?

该技能是 Go 语言开发的智能编排核心。它会在处理任何 Go 相关任务时(包括编码、代码审查、调试或项目设置)被自动触发,并智能加载 `samber/cc-skills-golang` 集合中最相关的子技能。通过分析任务语境,它能同时加载多个技能:例如编写 gRPC 服务时,同时加载 gRPC 编码、测试和错误处理技能;调试 panic 时,同时加载故障排查和安全性技能;进行安全审计时,则同时加载安全、lint 和安全性技能。此外,该技能还具备歧义消除能力,能够在两个技能看似重叠时(如性能 vs 基准测试 vs 故障排查,samber/lo vs mo vs ro,依赖注入集群,安全 vs 安全性)清晰界定其边界。它还支持配置模式,允许开发者将技能配置写入项目的 agent-config 文件(支持 CLAUDE.md、AGENTS.md、GEMINI.md、Cursor 规则或 Copilot 指令),并通过 `/golang-how-to configure` 命令实现项目级别的技能强制触发。该技能支持所有主流 AI 编码代理平台。

兼容平台✓Claude Code✓Codex CLI✓Cursor✓Gemini CLI
npx skills add https://github.com/samber/cc-skills-golang/tree/main/skills/golang-how-to

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

角色: 你是一个 Go 技能编排器。对于每个 Go 任务,识别所有相关技能并一起加载——一个任务很少只属于单个技能。

模式:

  • 编排模式 —— 对于任何 Go 编码、审查、调试或设置任务,同时加载主要技能以及所有适用的辅助技能。
  • 歧义消除模式 —— 当两个技能似乎重叠时,显示边界表。参见 disambiguation.md。
  • 配置模式 —— 将 golang-how-to 本身的始终加载指令,以及可选的 ## Required Go skills 块,写入项目的 agent-config 文件(CLAUDE.md、AGENTS.md 或等效文件)。遵循 project-config.md。

问题: 在配置模式中,通过环境的提问工具向用户提问——切勿使用纯文本叙述。一次只提一个问题,等待回答。如果环境没有提问工具,则以叙述形式呈现相同选项。

依赖: gopls —— go install golang.org/x/tools/gopls@latest;Claude Code 内置的 LSP 工具还需要 ENABLE_LSP_TOOL=1 和一个已配置的 Go 语言服务器(参见 使用 gopls 进行代码导航)。

技能加载

对于每个任务,同时加载主要技能和所有适用的辅助技能。不要等待——在开始时一起加载。

意图主要技能同时加载
设计 API,选择模式golang-design-patternsgolang-structs-interfaces,golang-naming
命名类型、函数或包golang-naminggolang-code-style
以惯用方式处理错误golang-error-handlinggolang-safety(nil 密集型代码)
编写 goroutine、channel、同步golang-concurrencygolang-context(如果涉及取消)
传递截止时间/取消操作golang-contextgolang-concurrency(如果涉及 goroutine)
设计结构体、嵌入、使用接口golang-structs-interfacesgolang-design-patterns
数据库查询和事务golang-databasegolang-error-handling,golang-security
构建 gRPC 服务golang-grpcgolang-testing,golang-error-handling
构建 GraphQL APIgolang-graphqlgolang-testing,golang-error-handling
构建 CLI 命令树golang-spf13-cobragolang-cli,golang-spf13-viper(如果涉及配置)
从标志/环境变量/文件分层配置golang-spf13-vipergolang-spf13-cobra
编写测试golang-testinggolang-stretchr-testify(如果使用 testify)
应用优化模式golang-performancegolang-benchmark(先测量)
使用 pprof / benchstat 进行测量golang-benchmarkgolang-performance(修复),golang-troubleshooting(根因)
调试 panic 或意外行为golang-troubleshootinggolang-safety,golang-benchmark(如果与性能相关)
生产环境监控golang-observabilitygolang-performance(如果违反 SLO)
审计安全漏洞golang-securitygolang-safety,golang-lint
审查格式和风格golang-code-stylegolang-naming,golang-lint
重构或重组现有代码golang-refactoringgolang-naming,golang-code-style,golang-project-layout
配置 golangci-lintgolang-lintgolang-code-style
编写 godoc / README / CHANGELOGgolang-documentationgolang-naming
设置新项目结构golang-project-layoutgolang-design-patterns,golang-dependency-injection,golang-lint
设置 CI/CD 管道golang-continuous-integrationgolang-lint,golang-security
选择库golang-popular-libraries相关的库特定技能
查找包的文档、版本、导入者或 CVEgolang-pkg-go-devgolang-dependency-management
导航、诊断或重构本地代码(定义、引用、重命名)golang-gopls—
采用新的 Go 语言特性golang-modernizegolang-lint
使用 samber/lo(切片/映射辅助函数)golang-samber-logolang-data-structures,golang-performance
使用 samber/oops(结构化错误)golang-samber-oopsgolang-error-handling
使用 log/sloggolang-samber-sloggolang-observability,golang-error-handling
使用依赖注入golang-dependency-injectiongolang-google-wire 或 golang-uber-dig 或 golang-uber-fx 或 golang-samber-do

上述所有技能标识符都是 samber/cc-skills-golang@<name> 的简称。

使用 gopls 进行代码导航

gopls 为 Go 提供语义级代码智能——跳转到定义、查找引用、诊断、包 API、符号搜索、重构。→ 参见 samber/cc-skills-golang@golang-gopls 技能以了解访问它的三种方式(其自身的 MCP 服务器、原生 LSP 工具及其 CLI)、完整能力矩阵以及高效的读/编辑工作流。

gopls 仅依据本地构建中存在且可解析的代码进行推理:你的工作区以及每个依赖项在 go.sum 中锁定的确切版本(包括 replace 指令)。对于任何不依赖于本地构建的事实——版本历史、许可证、生态范围内的导入者、尚未添加的包——请使用 golang-pkg-go-dev(godig)。参见下面的 godig vs gopls vs Context7 vs govulncheck 部分以了解完整边界。

godig vs gopls vs Context7 vs govulncheck

四个工具都可以回答“这个依赖项是否可以使用”,但它们之间的重叠并不像看起来那么大:

  • Context7 是一个通用的、跨语言的文档获取器——适用于没有更具体来源的情况。对于 Go 包或模块,godig 几乎总是更好的选择:它直接从 pkg.go.dev 拉取结构化的、Go 特定的数据——确切的版本、带有签名的导出符号、可运行的示例、imported-by 以及已知的漏洞——而不是 Context7 通用的抓取/精选文档,后者不公开该结构,并且可能会滞后或错过不太知名的 Go 模块。仅在依赖项的文档确实不存在或未在 pkg.go.dev 上索引时,才使用 Context7。
  • godig 回答有关已发布的生态的问题:任何 Go 包或模块,无论它是否已经在你的 go.mod 中——它调用远程的 pkg.go.dev API,从不接触你的本地检出。其 vulns 命令报告已知的、针对指定包/版本(孤立地)的 CVE,无论你的构建是否实际到达易受攻击的代码路径。
  • gopls(→ samber/cc-skills-golang@golang-gopls,通过其 MCP 服务器、原生 LSP 工具或其 CLI)回答有关你的特定构建的问题:你的代码加上每个依赖项,精确到锁定版本。
  • govulncheck 是基于 gopls 之上构建的,它将 gopls 的内省能力提升了一步:它不仅报告 CVE,还报告哪些 CVE 实际上从你的代码可达(通过调用图分析)。如果你已经使用了 gopls,那么你应该使用 govulncheck 而不是孤立的 godig vulns,以获得更可操作的结果。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

samber/golang-benchmark

Golang benchmarking, profiling, and performance measurement. Use when writing, running, or comparing Go benchmarks, profiling hot paths with pprof, interpreting CPU/memory/trace profiles, analyzing results with benchstat, setting up CI benchmark regression detection, or investigating production performance with Prometheus runtime metrics. Also use when the developer needs deep analysis on a specific performance indicator - this skill provides the measurement methodology, while `samber/cc-skills-golang@golang-performance` provides the optimization patterns.

samber/golang-cli

Golang CLI application development. Use when building, modifying, or reviewing a Go CLI tool — especially for command structure, flag handling, configuration layering, version embedding, exit codes, I/O patterns, signal handling, shell completion, argument validation, and CLI unit testing. Also triggers when code uses cobra, viper, or urfave/cli. For cobra-specific APIs → See `samber/cc-skills-golang@golang-spf13-cobra` skill; for viper configuration layering → See `samber/cc-skills-golang@golang-spf13-viper` skill.

samber/golang-code-style

Golang code style conventions — line length and breaking, variable declarations, control flow clarity, when comments help vs hurt. Use when writing or reviewing Go code, asking about style or clarity, or establishing project coding standards. Not for naming conventions (→ See `samber/cc-skills-golang@golang-naming` skill), linter configuration (→ See `samber/cc-skills-golang@golang-lint` skill), or doc comments (→ See `samber/cc-skills-golang@golang-documentation` skill).

samber/golang-concurrency

Golang concurrency patterns. Use when writing or reviewing concurrent Go code involving goroutines, channels, select, locks, sync primitives, errgroup, singleflight, worker pools, or fan-out/fan-in pipelines. Also triggers when you detect goroutine leaks, race conditions, channel ownership issues, or need to choose between channels and mutexes.

samber/golang-context

Idiomatic context.Context usage in Golang — propagation through API boundaries, cancellation, timeouts and deadlines, request-scoped values, context.WithoutCancel for background work outliving requests. Apply when designing context propagation across layers, debugging leaked or unexpired contexts, choosing between context.Background/TODO/WithoutCancel, or storing values in context. Not for code that merely accepts ctx as first parameter.

samber/golang-continuous-integration

CI/CD pipeline configuration using GitHub Actions for Golang projects — testing, linting, SAST, security scanning, code coverage, Dependabot, Renovate, GoReleaser, code review automation, and release pipelines. Use when setting up or improving Go project CI, configuring GitHub Actions workflows, adding linters or security scanners, automating dependency updates, or adding quality gates.

samber/golang-database

Comprehensive guide for Go database access — parameterized queries, struct scanning, NULLable columns, transactions, isolation levels, SELECT FOR UPDATE, connection pool, batch processing, context propagation, and migration tooling. Use when writing, reviewing, or debugging Golang code that interacts with PostgreSQL, MariaDB, MySQL, or SQLite; for database testing; or for questions about database/sql, sqlx, or pgx. Does NOT generate database schemas or migration SQL.

samber/golang-data-structures

Golang data structures — slices (internals, capacity growth, preallocation, slices package), maps (internals, hash buckets, maps package), arrays, container/list/heap/ring, strings.Builder vs bytes.Buffer, generic collections, pointers (unsafe.Pointer, weak.Pointer), and copy semantics. Use when choosing or optimizing Go data structures, implementing generic containers, using container/ packages, unsafe or weak pointers, or questioning slice/map internals.

samber/golang-dependency-injection

Comprehensive guide for dependency injection (DI) in Golang. Covers why DI matters (testability, loose coupling, separation of concerns, lifecycle management), manual constructor injection, and DI library comparison (google/wire, uber-go/dig, uber-go/fx, samber/do). Use this skill when designing service architecture, setting up dependency injection, refactoring tightly coupled code, managing singletons or service factories, or when the user asks about inversion of control, service containers, or wiring dependencies in Go. For a specific DI library, → See `samber/cc-skills-golang@golang-google-wire`, `samber/cc-skills-golang@golang-uber-dig`, `samber/cc-skills-golang@golang-uber-fx`, or `samber/cc-skills-golang@golang-samber-do` skills.

samber/golang-dependency-management

Dependency management strategies for Golang projects — go.mod management, installing/upgrading packages, Minimal Version Selection, vulnerability scanning, outdated dependency tracking, binary size analysis, Dependabot/Renovate setup, conflict resolution, and go.work workspaces. Use when adding, removing, or upgrading Go dependencies, auditing vulnerabilities, resolving version conflicts, or setting up automated dependency updates.

samber/golang-design-patterns

Idiomatic Golang design patterns — functional options, constructors, error flow and cascading, resource management and lifecycle, graceful shutdown, resilience, architecture, dependency injection, data handling, streaming, and more. Apply when explicitly choosing between architectural patterns, implementing functional options, designing constructor APIs, setting up graceful shutdown, applying resilience patterns, or asking which idiomatic Go pattern fits a specific problem.

samber/golang-documentation

Comprehensive documentation guide for Golang projects, covering godoc comments, README, CONTRIBUTING, CHANGELOG, Go Playground, Example tests, API docs, and llms.txt. Use when writing or reviewing doc comments, documentation, adding code examples, setting up doc sites, or discussing documentation best practices. Triggers for both libraries and applications/CLIs.

samber/golang-error-handling

Idiomatic Golang error handling — creation, wrapping with %w, errors.Is/As, errors.Join, custom error types, sentinel errors, panic/recover, the single handling rule, structured logging with slog, HTTP request logging middleware, and samber/oops for production errors. Built to make logs usable at scale with log aggregation 3rd-party tools. Apply when creating, wrapping, inspecting, or logging errors in Go code. For samber/oops specifics → See `samber/cc-skills-golang@golang-samber-oops` skill; for slog handler ecosystem → See `samber/cc-skills-golang@golang-samber-slog` skill.

samber/golang-google-wire

Compile-time dependency injection in Golang using google/wire — wire.NewSet, wire.Build, wire.Bind (interface→concrete), wire.Struct, wire.Value, wire.InterfaceValue, wire.FieldsOf, cleanup functions, //go:build wireinject injector files, and generated wire_gen.go. Apply when using or adopting google/wire, when the codebase imports `github.com/google/wire`, or when wiring an application graph at compile time via `wire.Build`. For runtime DI with reflection, see `samber/cc-skills-golang@golang-uber-dig` skill.

samber/golang-gopls

Golang semantic code intelligence via `gopls`, the official Go language server — go-to-definition, find references, call/implementation hierarchy, workspace symbol search, package API discovery, diagnostics, safe rename, refactors (extract/inline/fill/rewrite code actions), formatting, and generated tests. Reaches an agent via gopls's own MCP server (`go_*` tools), Claude Code's native `LSP` tool, or the `gopls` CLI. Use when navigating or refactoring Go code — jumping to a definition, finding call sites before a rename, understanding a file's or package's dependencies, running diagnostics after an edit, or extracting/inlining/renaming. Not for the published ecosystem — packages not in your `go.mod`, versions, licenses, importers — → See `samber/cc-skills-golang@golang-pkg-go-dev` skill (`godig`). Not for a whole-tree vulnerability audit → See `samber/cc-skills-golang@golang-security` skill (`govulncheck`).

samber/golang-graphql

Implements GraphQL APIs in Golang using gqlgen or graphql-go. Apply when building GraphQL servers, designing schemas, writing resolvers, handling subscriptions, or integrating GraphQL with existing Go HTTP services. Also apply when the codebase imports `github.com/99designs/gqlgen` or `github.com/graph-gophers/graphql-go`.

samber/golang-grpc

Provides gRPC usage guidelines, protobuf organization, and production-ready patterns for Golang microservices. Use when implementing, reviewing, or debugging gRPC servers/clients, writing proto files, setting up interceptors, handling gRPC errors with status codes, configuring TLS/mTLS, testing with bufconn, or working with streaming RPCs.

samber/golang-lint

Linting best practices and golangci-lint configuration for Golang projects — running linters, configuring .golangci.yml, suppressing warnings with nolint directives, interpreting lint output, and selecting linters. Use when configuring golangci-lint, asking about lint warnings or nolint suppressions, setting up code quality tooling, or choosing linters. Also use when the user mentions golangci-lint, go vet, staticcheck, or revive.

samber/golang-modernize

Modernize Golang code to use recent language features, standard library improvements, and idiomatic patterns. Trigger proactively when writing or reviewing Go code and old-style patterns are detected, or when encountering a deprecation warning. Also use when the user explicitly asks for modernization, a Go version upgrade, or a CI/tooling refresh.

samber/golang-naming

Go (Golang) naming conventions — covers packages, constructors, structs, interfaces, constants, enums, errors, booleans, receivers, getters/setters, functional options, acronyms, test functions, and subtest names. Use this skill when writing new Go code, reviewing or refactoring, choosing between naming alternatives (New vs NewTypeName, isConnected vs connected, ErrNotFound vs NotFoundError, StatusReady vs StatusUnknown at iota 0), debating Go package names (utils/helpers anti-patterns), or asking about Go naming best practices. Also trigger when the user mentions MixedCaps vs snake_case, ALL_CAPS constants, Get-prefix on getters, or error string casing. Do NOT use for general Go implementation questions that don't involve naming decisions.

相关技能