go-conventions
Go rules for package layout, error wrapping, context propagation, structured logging, configuration and license headers. Use when writing, reviewing or refactoring Go code.
SKILL.mdskills/go/go-conventions/SKILL.md
GitHubGo conventions
Baseline conventions for Go code in services, CLIs and libraries.
When to use
- Writing, reviewing or refactoring Go code.
- Setting up a new Go module.
Rules
Layout
cmd/<binary>/main.go: wiring only. Flags, config, construction, shutdown.internal/: everything else, by default.pkg/: only when another module imports it.- No
util,commonorhelperspackage. Split by domain, not by layer.
Errors
- Wrap with
fmt.Errorf("load config: %w", err). Lowercase, no punctuation, no "failed to". - Compare a sentinel with
errors.Is. Useerrors.Aswhen a caller needs fields off a typed error. - No panic in library code.
- Never log and return the same error. The caller that stops the chain logs it.
- Ignore a return value only as
_, with a comment saying why.
Context
ctxis the first parameter on anything doing IO, blocking or spawning work.context.Background()only inmainand in tests.- No context stored in a struct field outside a request scoped struct.
context.WithTimeoutat IO boundaries.- Context values carry request metadata only, keyed by an unexported type. Never a dependency.
Logging
log/slogonly. Built once inmain, injected everywhere else.- No package level logger, no
slog.Default()in library code. - Key value pairs, not formatted strings.
Infofor lifecycle,Warnfor degradation,Errorfor a handled failure. Nolog.Fatal(the standard librarylogpackage) outsidemain.- Never log a secret, a token or a request body.
Config and state
- One struct, loaded from the environment and flags once in
main, validated fully before startup. - Aggregate every invalid field into one error. No scattered
os.Getenv. - A secret comes from the environment or a file path, never committed.
- No global mutable state. A package level variable holds only a constant, a sentinel, a compiled regex or a template.
- No
initside effects. A constructor takes its dependencies as parameters.
Dependencies
- Define an interface at the consumer, not the implementation. One or two methods, named for behavior.
- Accept an interface, return a struct.
- No interface until a second implementation or a test fake needs one.
- Prefer the standard library. Every third party dependency needs a reason in the pull request.
go.moddeclares the oldest supported Go version. Rungo mod tidybefore commit and commitgo.sum.
License header
- Every source file carries the license header matching the repository's license, as the first comment after any build tags.
Guardrails
internal/holds the logic,cmd/only wires,pkg/justified or absent.- Errors wrapped with
%w, no panic in library code, no error logged more than once. ctxis the first parameter, nocontext.Background()outsidemainand tests.log/sloginjected, no package level logger, no secret ever logged.- Config loaded and validated once, no global mutable state, no
initside effects. - Interfaces small and defined at the consumer, each dependency justified.
- License header present on every source file.