Back to rules

Go Backend Development

Verified Intermediate ~390 tokens MIT License
723 upvotes 24,300 views by ClaudeHub Team
Works with:
go docker kubernetes postgresql
CLAUDE.md
You are an expert in Go, backend services, and building scalable distributed systems.Key Principles- Write simple, readable Go code following the Go proverbs.- Prefer composition over inheritance; use interfaces for abstraction.- Handle errors explicitly; never ignore returned errors.- Use goroutines and channels for concurrent operations.Code Style- Follow the official Go style guide and use gofmt/goimports.- Keep functions short and focused on a single task.- Use meaningful variable names; short names only for short scopes.- Avoid package-level state; prefer dependency injection.Error Handling- Always check and handle errors; use errors.Is/errors.As for error checking.- Wrap errors with context using fmt.Errorf with %w verb.- Create custom error types for domain-specific errors.- Use sentinel errors sparingly; prefer error behavior checking.Concurrency- Use goroutines for concurrent operations; keep them lightweight.- Prefer channels for communication; share memory by communicating.- Use context.Context for cancellation and timeout propagation.- Avoid goroutine leaks; always have a way to terminate goroutines.Project Structure- Use standard project layout: cmd/, internal/, pkg/, api/.- Keep main.go minimal; delegate to internal packages.- Use interfaces in consuming packages, not producing packages.- Group related functionality into cohesive packages.Testing- Write table-driven tests for comprehensive coverage.- Use testify for assertions and mocking when needed.- Test behavior, not implementation; use interfaces for test doubles.- Benchmark performance-critical code with testing.B.Dependencies- Use Go modules for dependency management.- Prefer standard library; add dependencies judiciously.- Pin versions in go.mod; use go mod tidy regularly.- Consider using wire for dependency injection in large projects.

How to use this rule

  1. Copy the content above
  2. Create or open your CLAUDE.md file in your project root
  3. Paste the content and customize as needed
  4. Claude Code will automatically use these instructions