Skip to main content
This page extends Common Rules with Go specific content.Applies to: **/*.go, **/go.mod, **/go.sum

Coding Style

Formatting

gofmt and goimports are mandatory

No style debates — the Go toolchain enforces formatting

Design Principles

Functions should accept interfaces (flexible) and return concrete types (specific).
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.As

Reference

golang-patterns

See skill for comprehensive Go idioms and patterns

Testing

Framework

Use the standard go test with table-driven tests.

Race Detection

Always run with the -race flag:
Race conditions can cause subtle, hard-to-debug issues in production. Always test with -race during development.

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 use context.Context for timeout control:
Timeouts prevent goroutines from hanging indefinitely and consuming resources

Hooks

PostToolUse Hooks

Configure in ~/.claude/settings.json:
Automatically runs gofmt and goimports on Go files after edits.
Example hook:
Runs go vet to catch common mistakes.
Runs staticcheck for more comprehensive analysis.

Agent Support

go-reviewer

Go code review specialist

go-build-resolver

Go build error resolution

Common Rules

Language-agnostic base rules

Hooks Overview

Complete hook system reference