Community程式設計與開發github.com

samber/golang-gopls

透過官方 Go 語言伺服器 `gopls` 提供語意級程式碼智能:前往定義、尋找參考、呼叫/實作階層、工作區符號搜尋、套件 API 探索、診斷、安全重新命名、重構(提取/內聯/填寫/重寫程式碼動作)、格式化與生成測試。

golang-gopls 是什麼?

此技能讓 AI 代理(如 Claude Code、Cursor、Codex)深入理解 Go 程式碼的語意結構,而非僅依賴文字搜尋。它透過三種方式與 `gopls` 互動:專為代理設計的 MCP 伺服器(依名稱/路徑操作)、Claude Code 內建的 `LSP` 工具(自動推送診斷)、以及 `gopls` CLI(適合腳本檢查)。核心功能包括精準導航(前往定義、階層)、程式碼發現(工作區佈局、符號搜尋、依賴 API)、安全重構(重新命名、提取/內聯、填寫結構體/switch、實現介面)、以及每次編輯後的自動診斷。使用此技能時應遵循建議的讀取/編輯工作流程:先讀取工作區與符號資訊,再進行修改,最後執行診斷與測試,以確保程式碼品質。對於未納入專案 `go.mod` 的套件(如版本、授權、CVE),請改用 `samber/cc-skills-golang@golang-pkg-go-dev` 技能;對於全面漏洞審計,則請改用 `samber/cc-skills-golang@golang-security` 技能。

相容平台✓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 指令)。對於不在此建構中的套件(版本、文件、授權、尚未加入的 CVEs),請改用 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 <更改的套件路徑>——除非明確要求,否則不要執行 ./...。

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.

相關技能