This page extends Common Rules with Go specific content.Applies to:
**/*.go, **/go.mod, **/go.sumCoding Style
Formatting
gofmt and goimports are mandatory
No style debates — the Go toolchain enforces formatting
Design Principles
Accept interfaces, return structs
Accept interfaces, return structs
Functions should accept interfaces (flexible) and return concrete types (specific).
Keep interfaces small (1-3 methods)
Keep interfaces small (1-3 methods)
Small, focused interfaces are easier to implement and test.
Error Handling
Always wrap errors with context:Use
%w to wrap errors (Go 1.13+) for error unwrapping with errors.Is and errors.AsReference
golang-patterns
See skill for comprehensive Go idioms and patterns
Testing
Framework
Use the standardgo test with table-driven tests.
Race Detection
Always run with the-race flag:
Coverage
Reference
golang-testing
See skill for detailed Go testing patterns and helpers
Patterns
Functional Options
Small Interfaces
Define interfaces where they are used, not where they are implemented.Dependency Injection
Use constructor functions to inject dependencies:Reference
golang-patterns
See skill for comprehensive Go patterns including concurrency, error handling, and package organization
Security
Secret Management
Security Scanning
Use gosec for static security analysis:Context & Timeouts
Always usecontext.Context for timeout control:
Timeouts prevent goroutines from hanging indefinitely and consuming resources
Hooks
PostToolUse Hooks
Configure in~/.claude/settings.json:
gofmt/goimports: Auto-format .go files after edit
gofmt/goimports: Auto-format .go files after edit
Automatically runs gofmt and goimports on Go files after edits.Example hook:
go vet: Run static analysis after editing .go files
go vet: Run static analysis after editing .go files
Runs
go vet to catch common mistakes.staticcheck: Run extended static checks on modified packages
staticcheck: Run extended static checks on modified packages
Runs staticcheck for more comprehensive analysis.
Agent Support
go-reviewer
Go code review specialist
go-build-resolver
Go build error resolution
Related
Common Rules
Language-agnostic base rules
Hooks Overview
Complete hook system reference