一键导入
golang-patterns
Idiomatic Go patterns, best practices, and conventions for building robust, efficient, and maintainable Go applications.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Idiomatic Go patterns, best practices, and conventions for building robust, efficient, and maintainable Go applications.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
ann-benchmarks フレームワーク規約(algos.yaml/HDF5/Pareto frontier分析)と ANN ベクトル検索の SIMD 距離カーネル最適化における落とし穴パターン(AVX2/AVX-512 ディスパッチャ検証・量子化の数学的等価性確認・部分集合とフルスケールの混同防止)。ArcFlare/NGT/NGTAQ 等の ANN R&D 作業時の参照用。実装作業自体は ann-perf-engineer agent に委譲する。
Use this skill to measure performance baselines, detect regressions before/after PRs, and compare stack alternatives.
Claude API / Anthropic Go SDK usage patterns, prompt caching, streaming, tool use, and model selection for Go applications.
C++ coding standards based on the C++ Core Guidelines (isocpp.github.io). Use when writing, reviewing, or refactoring C++ code to enforce modern, safe, and idiomatic practices.
Deployment workflows, CI/CD pipeline patterns, Docker containerization, health checks, rollback strategies, and production readiness checklists for web applications.
非推奨・後方互換用リダイレクト。旧 dig(コードベース深掘り分析→設計インタビュー→実装計画→自律実行)は swarm-loop に完全統合された。/dig と入力された場合は本ファイルの指示に従い、そのまま swarm-loop skill を 同じ目標・同じ引数で起動すること。dig 独自のロジックはここには存在しない。
| name | golang-patterns |
| description | Idiomatic Go patterns, best practices, and conventions for building robust, efficient, and maintainable Go applications. |
| origin | ECC |
Idiomatic Go patterns and best practices for building robust, efficient, and maintainable applications.
Detailed code examples for every section below live in reference.md — this file holds the rules, decision criteria, and quick-reference tables; reference.md holds the full "how" (code samples, anti-pattern walkthroughs, tooling config).
bytes.Buffer, a sync.Mutex-guarded counter).See reference.md → "Core Principles" for the Good/Bad code pairs.
| SOLID | Go 解釈 | ルール |
|---|---|---|
| S Single Responsibility | パッケージ凝集度 | 1 package = 1 責務 |
| O Open/Closed | インターフェース拡張 | 既存型を変更せず interface + composition で拡張 |
| L Liskov Substitution | インターフェース契約 | 実装は interface の暗黙契約(戻り値の意味・エラー条件)を完全に守る |
| I Interface Segregation | 最小インターフェース | → 「Interface Design」セクション参照 |
| D Dependency Inversion | 依存性逆転 | → 「Define Interfaces Where They're Used」参照 |
See reference.md → "Design Principles (Go-SOLID)" for SRP/OCP/LSP code examples (net/http-style package split, Middleware chaining, io.Writer contract integrity).
fmt.Errorf("...: %w", err) so callers get a diagnosable chain.ValidationError) plus sentinel errors (ErrNotFound, ...) for common cases.errors.Is for sentinel comparison and errors.As for type extraction; never branch on err.Error() string content.See reference.md → "Error Handling Patterns" for full code.
jobs channel, sync.WaitGroup for completion.context.Context through I/O calls, always defer cancel().SIGINT/SIGTERM, call server.Shutdown(ctx) with a bounded timeout.golang.org/x/sync/errgroup to fan out and collect the first error.select on ctx.Done() so a goroutine can always exit when nobody is listening.See reference.md → "Concurrency Patterns" for full code, including the leaky-vs-safe fetch comparison.
Reader, Writer, Closer) composed into larger ones as needed.Flusher) via a type assertion instead of forcing every implementation to support it.See reference.md → "Interface Design" for full code.
cmd/ for entry points, internal/ for private application code, pkg/ for a public client, api/ for schema definitions.Service.init()-populated globals.See reference.md → "Package Organization" for the full directory tree and naming examples.
Option functions to configure a struct's optional fields while keeping required fields in the constructor signature.See reference.md → "Struct Design" for full code.
make([]T, 0, len(items)) instead of letting append grow the slice repeatedly.strings.Builder, or strings.Join when the whole slice is available upfront.See reference.md → "Memory and Performance" for full code.
# Build and run
go build ./...
go run ./cmd/myapp
# Testing
go test ./...
go test -race ./...
go test -cover ./...
# Static analysis
go vet ./...
staticcheck ./...
golangci-lint run
# Module management
go mod tidy
go mod verify
# Formatting
gofmt -w .
goimports -w .
See reference.md → "Go Tooling Integration" for the recommended .golangci.yml linter configuration.
| Idiom | Description |
|---|---|
| Accept interfaces, return structs | Functions accept interface params, return concrete types |
| Errors are values | Treat errors as first-class values, not exceptions |
| Don't communicate by sharing memory | Use channels for coordination between goroutines |
| Make the zero value useful | Types should work without explicit initialization |
| A little copying is better than a little dependency | Avoid unnecessary external dependencies |
| Clear is better than clever | Prioritize readability over cleverness |
| gofmt is no one's favorite but everyone's friend | Always format with gofmt/goimports |
| Return early | Handle errors first, keep happy path unindented |
panic for ordinary control flow — return an error instead.context.Context inside a struct — it must be the first parameter of the function that needs it.See reference.md → "Anti-Patterns to Avoid" for full code.
Remember: Go code should be boring in the best way - predictable, consistent, and easy to understand. When in doubt, keep it simple.