★ Featured
Go Idioms & Best Practices
Idiomatic Go with error handling, interfaces, testing, and concurrency patterns.
CLAUDE.md
# Go Idioms & Best Practices
You are an expert in Go 1.22+, standard library, and idiomatic Go patterns.
Error Handling:
- ALWAYS check errors; never ignore them with _
- Wrap errors with context: fmt.Errorf("failed to fetch user: %w", err)
- Use errors.Is() and errors.As() for error checking
- Create custom error types for domain errors
- Return errors, don't panic (panic is for truly unrecoverable situations)
Code Style:
- Accept interfaces, return structs
- Keep functions short: under 40 lines
- Use named return values only for documentation, not naked returns
- Context as first parameter in all functions: func DoThing(ctx context.Context, ...) error
- Use meaningful variable names; avoid single-letter except in very short scopes
Concurrency:
- Share memory by communicating, don't communicate by sharing memory
- Use channels for coordination between goroutines
- Always manage goroutine lifecycle: ensure they can be cancelled
- Use sync.WaitGroup for fan-out/fan-in patterns
- Use context.WithCancel/WithTimeout for cancellation
Testing:
- Use table-driven tests for comprehensive coverage
- Use testing.T.Run for subtests
- Test behavior, not implementation
- Use interfaces for testability (dependency injection)
- Use testify/assert for cleaner assertions
HTTP:
- Use http.Handler and http.HandlerFunc interfaces
- Implement middleware as function wrappers
- Use structured logging with slog package
- Graceful shutdown with signal handling
- Use context for request-scoped values and cancellation
Add to your project root CLAUDE.md file, or append to an existing one.