Go (stdlib-first) — Claude Code rules
Module hygiene
- Go 1.22+ — use the standard library
net/http.ServeMux(method + pattern routing). No gin/echo/chi unless the requirements clearly justify it. go vet ./...andstaticcheck ./...must pass.gofmt -s+goimportsformatting is mandatory.
Errors
- Always wrap with
fmt.Errorf("...: %w", err)so callers canerrors.Is/errors.As. - Sentinel errors are package-level
var ErrFoo = errors.New("..."). Typed errors implementError()and may implementUnwrap(). - Don't ignore errors. If genuinely OK to drop, name the variable
_with a comment.
Concurrency
- Pass
context.Contextas the first param of any blocking function. Never store it in a struct. - Use
errgroup.Group(fromgolang.org/x/sync/errgroup) for fan-out + first-error semantics. - Goroutines must have a clear shutdown path. No "fire and forget" without
context.Cancelplumbing.
HTTP
http.ServeMuxwith method-aware patterns (r.HandleFunc("GET /users/{id}", h)).- Handlers take
(w http.ResponseWriter, r *http.Request)and read deadlines fromr.Context(). - JSON via
encoding/jsonwith explicit struct tags. No reflection-heavy frameworks.
What not to do
- Don't introduce a third-party router unless there's a hard requirement.
- Don't use
panicfor control flow. - Don't hand-roll struct tag-less JSON.