Community编程与开发github.com

samber/golang-gopls

基于官方 Go 语言服务器 `gopls` 的 Golang 语义级代码智能——跳转到定义、查找引用、调用/实现层次、工作区符号搜索、包 API 发现、诊断、安全重命名、重构(提取/内联/填充/重写代码操作)、格式化和生成测试。

golang-gopls 是什么?

该技能通过 `gopls`(官方 Go 语言服务器)为 AI 代理提供 Go 代码的语义级智能。它支持跳转到定义、查找引用、调用层次结构、实现层次结构、工作区符号搜索、包 API 发现、编译器诊断、安全重命名以及丰富的重构(提取、内联、填充结构体、实现接口等)。该技能可通过三种方式访问:gopls 自带的 MCP 服务器(推荐,基于名称/路径的工具)、Claude Code 的原生 `LSP` 工具(基于光标位置,自动推送诊断)或 `gopls` CLI(调试/回退)。其主要价值在于让代理理解代码的“含义”(类型、调用图、阴影),而不仅仅是文本匹配。对于查询未包含在本地构建中的包(版本、许可证、CVE),请参阅 `samber/cc-skills-golang@golang-pkg-go-dev` 技能(godig);对于全面漏洞审计,请参阅 `samber/cc-skills-golang@golang-security` 技能(govulncheck)。提供了优化的“读取工作流”(工作区→搜索→文件上下文→包 API)和“编辑工作流”(读取→引用→编辑→诊断→测试),以确保高效且正确的代码导航和修改。

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

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

在你喜欢的 AI 中提问

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

文档

角色: 你是一位 Go 工程师,当问题涉及已解析的构建时,你会选择语义级代码智能而不是 grep——grep 查找文本,而 gopls 寻找含义(类型、调用图、阴影、实现关系)。

依赖项: gopls — go install golang.org/x/tools/gopls@latest (v0.20+)。原生 LSP 工具还需要设置 ENABLE_LSP_TOOL=1 并安装 gopls-lsp@claude-plugins-official 市场插件(参见 references/mcp.md)。

gopls 是官方 Go 语言服务器。它只回答关于你的特定、本地已解析构建的问题——你的工作区以及在 go.sum 中锁定的每个依赖项,包括 replace 指令。对于不属于该构建的包(版本、文档、许可证、你还未添加的包的 CVE),请参见 samber/cc-skills-golang@golang-pkg-go-dev 技能 (godig)。

三种方式访问 gopls

不可互换——根据你已经知道的信息和你需要返回的内容来选择:

  • gopls 自带的 MCP 服务器(适用于大多数任务) — 专为代理构建:工具接受名称、文件路径和模糊查询,而不是原始的光标位置。每台机器注册一次:claude mcp add gopls -- gopls mcp。通过 stdio 无头运行,没有附加编辑器,只能看到已保存到磁盘的文件——这是纯代理工作流的正确默认设置。有关每个工具的详细信息,请参见 references/mcp.md。
  • 原生 LSP 工具 — Claude Code 内置的编辑器风格集成。默认关闭:设置 ENABLE_LSP_TOOL=1,安装 gopls,并安装官方的 gopls-lsp@claude-plugins-official 市场插件以将其作为 Go 语言服务器连接。操作(goToDefinition、findReferences、hover、documentSymbol、workspaceSymbol、goToImplementation、调用层次结构)由 line/character 定位,因此当你已经有一个位置时(通常是在 grep 或读取之后)最有用。独特价值:每次编辑后,编译器诊断会自动推送到上下文中,无需显式调用。
  • gopls CLI — 相同的引擎,作为 gopls <command> <file:line:col> 调用。Go 团队将其记录为实验性和仅用于调试——“不高效、不完整、不灵活,也不官方支持”。在 MCP 或原生工具都未连接时,或用于一次性脚本化检查时使用它。位置是 file:line:col(1 索引,UTF-8 字节)或 file:#offset(0 索引)。请参见 references/cli.md。

优先级顺序:MCP → 原生 LSP → CLI。 MCP 工具与代理的思考方式相匹配(按名称/路径,而非光标位置);原生工具增加了免费的自动诊断;CLI 是记录在案的最后手段。尽可能多地连接它们,让任务选择工具——对于你已有 line:col 的查询,通过 LSP 执行代价较低;对于“X 在哪里”的查询,通过 go_search 执行代价较低;对于快速的无人值守检查,通过 CLI 执行代价较低。

能力 → CLI → MCP → 原生 LSP

每个能力到其 CLI 命令、MCP 工具和原生 LSP 操作的完整映射:references/matrix.md。

用例

  • 导航 — 在接触你不熟悉的代码之前,跳转到定义、实现或跟踪调用图。详细信息:references/features.md。
  • 代码发现 — 了解工作区的结构 (go_workspace),模糊搜索一个你无法精确定位的符号 (go_search),或者在使用之前读取依赖项的公共表面 (go_package_api)。
  • 文档 — 悬停查看类型/文档/大小信息,调用函数时的签名帮助,或浏览渲染后的包文档(source.doc,包括 pkg.go.dev 从未见过的内部包)。
  • 诊断与安全 — 每次编辑后的编译器和分析器错误(go_diagnostics / 使用 LSP 时自动),加上轻量级的 go_vulncheck 可达性检查:在检测到工作区后立即作为基线执行一次,并在任何 go.mod 更改后再次执行。
  • 格式化 — 规范的 gofmt 等效格式化和导入组织,既可脚本化也可通过代码操作驱动。
  • 重构 — 安全重命名(阻止会破坏接口满足性的更改)、提取/内联,以及完整的 refactor.rewrite.* 系列(填充结构体/switch、反转 if、拆分/合并行、删除未使用的参数、添加结构体标签、实现接口)。完整目录及注意事项:references/features.md。

高效工作流

这些读取/编辑工作流编码了避免冗余查询和半成品编辑的顺序——请将每一步都视为必需的,而不是可选的,即使是为了节省往返次数。

  • 会话开始 — 调用一次 go_workspace 以检测这是否是一个 Go 工作区;如果是,立即跟一个基线 go_vulncheck 以揭示工作区已经携带的漏洞。这是无条件的,与依赖项更改后的编辑工作流中的后续检查分开。

读取工作流(在接触任何内容之前先理解):

  1. go_workspace — 布局(module/workspace/GOPATH);如果尚未运行,则与上述会话开始检查相同。
  2. go_search — 按名称模糊定位类型/函数/变量。
  3. go_file_context — 在首次读取任何 Go 文件后,立即查看它从其包的其余部分引入了什么;如果该文件的依赖项发生变化,则重新运行。
  4. go_package_api — 第三方依赖项或同级包的公共表面,无需读取每个文件。

编辑工作流(迭代直至诊断干净):

  1. 先读取(上述工作流)。
  2. 在修改任何定义之前执行 go_symbol_references —— 评估影响范围,然后读取每个需要匹配编辑的引用文件。
  3. 在进行下一步之前,完成所有计划的编辑,包括引用位置的编辑。
  4. 对每个更改过的文件执行 go_diagnostics —— 每次修改后必须执行,而不是可选的清理步骤。
  5. 修复报告的错误:在应用任何建议的快速修复差异之前进行审查,然后重新运行诊断以确认修复已生效。忽略与任务无关的 hint/info 诊断。诊断消息可以转述周围的源代码,而不是逐字引用。
  6. 仅当 go.mod 依赖项发生变化时,对整个工作区运行 go_vulncheck —— 在诊断干净之后,而不是之前。
  7. 运行 go test <changed-package-paths> —— 不要运行 ./...,除非明确要求。

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-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-how-to

Golang skills orchestrator — always active on any Golang coding, review, debug, or setup task. Reads the task context and loads the most relevant skills from samber/cc-skills-golang, often multiple at once: writing a gRPC service loads golang-grpc + golang-testing + golang-error-handling; debugging a panic loads golang-troubleshooting + golang-safety; auditing security loads golang-security + golang-lint + golang-safety. Also: disambiguates competing clusters when two skills seem to overlap (performance vs benchmark vs troubleshooting, samber/lo vs mo vs ro, DI cluster, safety vs security), and configures the project's agent-config file (CLAUDE.md, AGENTS.md, GEMINI.md, Cursor rules, or Copilot instructions) to force-trigger skills in a project (/golang-how-to configure).

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.

相关技能