Go Engineering Standard
Purpose
Define the rules every Go file in this organization must satisfy — project layout,
error handling, concurrency, logging, and API style — so that Go written by different
teams is indistinguishable in quality. The skill encodes the current stable toolchain
(Go 1.26+; the 1.27 line is current upstream) and modern idioms: modules, generics,
slog, context, and error wrapping.
When to Use / When NOT to Use
Use when:
- Writing, reviewing, or refactoring any
.go file, go.mod, or Go CI job.
- Designing a Go library API (options, interfaces, error values).
- Onboarding a service to the shared logging/concurrency standards.
Do NOT use when:
- The task is Java or Python — use
language-specific/java or language-specific/python.
- The task is cross-language (REST shape, pipelines) — use the owning category skill;
this skill covers Go-specific expression of those rules only.
Prerequisites & Assumptions
- Go 1.26+ (module
go directive may state the minimum supported version; CI tests
the two most recent minor releases).
golangci-lint v2 per static-analysis; govulncheck in CI per security.
- Standard layout assumed:
cmd/<binary>/, internal/, pkg/ (only for truly
public libraries), api/ for proto/OpenAPI assets.
Step-by-Step Workflow
- Scaffold —
go mod init, create cmd/ + internal/, add .golangci.yml
(from static-analysis), add Makefile targets build/test/lint/vet.
- Design packages — small packages named by what they provide (
package throttle
not package util); consumer-side interfaces; constructors return fully valid
values.
- Write functions —
context.Context first parameter for I/O paths; guard
clauses; errors wrapped with %w and lowercase, punctuation-free messages.
- Handle concurrency — share memory by communicating; every goroutine has an
owner that guarantees termination (context, done channel, or WaitGroup).
- Log observably —
log/slog with typed attributes; no fmt.Println debugging
residue (rule details in observability).
- Verify —
go vet, golangci-lint run, go test -race ./..., table-driven
tests per testing/unit-testing, go build ./... green.
Enterprise Rules & Standards (MUST / MUST NOT)
- MUST wrap errors with context:
fmt.Errorf("parse config: %w", err); sentinel
errors (var ErrNotFound = errors.New(...)) or typed errors for caller decisions;
errors.Is/errors.As at boundaries. Never compare error strings.
- MUST pass
context.Context as the first parameter of any function performing
I/O, and never store contexts in structs except for request-scoped types.
- MUST use
log/slog with typed key-value attributes for logging; the context
variant (slog.InfoContext) in request paths.
- MUST make every goroutine terminable: launch with an owner-tracked
context.Context, done channel, or WaitGroup; go test -race is a required
gate for concurrent code.
- MUST validate inputs at package boundaries; return
(T, error), never panics,
for expected failures. Panic only for programmer bugs (impossible states).
- MUST keep
go.mod tidy: minimal direct dependencies, go mod tidy clean, no
replace directives in published modules.
- SHOULD use generics for type-safe containers/helpers; do not force generics
where an interface or concrete type is simpler.
- SHOULD prefer
any over interface{} (Go 1.18+), min/max builtins
(1.21+), loop-var semantics of 1.22+ (no v := v copies), and for range int
(1.22+) for counted loops.
- SHOULD use
t.Context() in tests (Go 1.24+) for cancellation-aware test code.
- MUST NOT use
interface{}/any to dodge type safety in exported APIs.
- MUST NOT use
math/rand for security values (crypto/rand), or MD5/SHA-1 for
integrity — see security.
- MUST NOT ignore errors with
_ in production code paths.
- MUST NOT call
os.Exit or log.Fatal inside libraries; return errors and let
main decide.
Examples
Good — idiomatic service call chain (compiles under Go 1.26):
package orders
import (
"context"
"errors"
"fmt"
"log/slog"
"time"
)
var ErrOrderNotFound = errors.New("order not found")
type Order struct {
ID string
Status string
}
// Cancel marks a confirmed order as cancelled; other statuses are rejected.
func (o *Order) Cancel() error {
if o.Status != "confirmed" {
return fmt.Errorf("cancel order %s: status %q is not cancellable", o.ID, o.Status)
}
o.Status = "cancelled"
return nil
}
type Store interface {
FindOrder(ctx context.Context, id string) (Order, error)
}
type Service struct {
store Store
logger *slog.Logger
}
func NewService(store Store, logger *slog.Logger) *Service {
return &Service{store: store, logger: logger}
}
// Cancel cancels the order with the given id before its cutoff time.
func (s *Service) Cancel(ctx context.Context, id string) error {
const op = "orders.Cancel"
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
order, err := s.store.FindOrder(ctx, id)
switch {
case errors.Is(err, ErrOrderNotFound):
return fmt.Errorf("%s: %w", op, err)
case err != nil:
return fmt.Errorf("%s: find order: %w", op, err)
}
if err := order.Cancel(); err != nil {
return fmt.Errorf("%s: cancel order %s: %w", op, id, err)
}
s.logger.InfoContext(ctx, "order cancelled",
slog.String("order_id", id))
return nil
}
Bad — the classic violations, all in one function:
func (s *Service) Cancel(id string) error {
order, _ := s.store.FindOrder(nil, id) // ignored error, nil context
if order.ID == "" { // sentinel check by field, not error
fmt.Println("not found") // fmt instead of slog
}
go s.notify(id) // unowned goroutine, race-prone
return nil
}
Why it fails: swallowed error breaks errors.Is handling upstream; nil context
panics in store implementations; fmt.Println is not structured; the goroutine can
outlive the request and races on shared state.
Checklist
Common Pitfalls
- Nil-context calls —
context.Background() with a wiring comment is the
honest stand-in when no caller context exists yet; nil never is.
- Loop-variable paranoia on modern Go —
v := v copies are unnecessary since
Go 1.22; remove them, keep them only in generics weirdness with proof.
- time.After in loops/selects — leaks timers until fire; use
time.NewTimer
with Reset for recurring waits.
- Error-message capitalization — Go errors read as sentences mid-flow;
"Parse config: ..." and trailing punctuation are wrong.
- init() abuse — implicit wiring hides dependencies; construct explicitly.
References
1---2name: golang-23description: 'Enforces modern enterprise Go — Go 1.26+ modules and generics, context propagation, error wrapping with %w, slog structured logging, and concurrency safety. Trigger with any "write/review/refactor Go code" request or "is this idiomatic Go".'4metadata:5 source: https://skillmd.com/skills/pnguyen215/golang-26---78# Go Engineering Standard910## Purpose1112Define the rules every Go file in this organization must satisfy — project layout,13error handling, concurrency, logging, and API style — so that Go written by different14teams is indistinguishable in quality. The skill encodes the current stable toolchain15(Go 1.26+; the 1.27 line is current upstream) and modern idioms: modules, generics,16`slog`, `context`, and error wrapping.1718## When to Use / When NOT to Use1920Use when:2122- Writing, reviewing, or refactoring any `.go` file, `go.mod`, or Go CI job.23- Designing a Go library API (options, interfaces, error values).24- Onboarding a service to the shared logging/concurrency standards.2526Do NOT use when:2728- The task is Java or Python — use `language-specific/java` or `language-specific/python`.29- The task is cross-language (REST shape, pipelines) — use the owning category skill;30 this skill covers Go-specific expression of those rules only.3132## Prerequisites & Assumptions3334- Go 1.26+ (module `go` directive may state the minimum supported version; CI tests35 the two most recent minor releases).36- `golangci-lint` v2 per `static-analysis`; `govulncheck` in CI per `security`.37- Standard layout assumed: `cmd/<binary>/`, `internal/`, `pkg/` (only for truly38 public libraries), `api/` for proto/OpenAPI assets.3940## Step-by-Step Workflow41421. **Scaffold** — `go mod init`, create `cmd/` + `internal/`, add `.golangci.yml`43 (from `static-analysis`), add `Makefile` targets `build/test/lint/vet`.442. **Design packages** — small packages named by what they provide (`package throttle`45 not `package util`); consumer-side interfaces; constructors return fully valid46 values.473. **Write functions** — `context.Context` first parameter for I/O paths; guard48 clauses; errors wrapped with `%w` and lowercase, punctuation-free messages.494. **Handle concurrency** — share memory by communicating; every goroutine has an50 owner that guarantees termination (context, done channel, or WaitGroup).515. **Log observably** — `log/slog` with typed attributes; no `fmt.Println` debugging52 residue (rule details in `observability`).536. **Verify** — `go vet`, `golangci-lint run`, `go test -race ./...`, table-driven54 tests per `testing/unit-testing`, `go build ./...` green.5556## Enterprise Rules & Standards (MUST / MUST NOT)5758- **MUST** wrap errors with context: `fmt.Errorf("parse config: %w", err)`; sentinel59 errors (`var ErrNotFound = errors.New(...)`) or typed errors for caller decisions;60 `errors.Is`/`errors.As` at boundaries. Never compare error strings.61- **MUST** pass `context.Context` as the first parameter of any function performing62 I/O, and never store contexts in structs except for request-scoped types.63- **MUST** use `log/slog` with typed key-value attributes for logging; the `context`64 variant (`slog.InfoContext`) in request paths.65- **MUST** make every goroutine terminable: launch with an owner-tracked66 `context.Context`, `done` channel, or `WaitGroup`; `go test -race` is a required67 gate for concurrent code.68- **MUST** validate inputs at package boundaries; return `(T, error)`, never panics,69 for expected failures. Panic only for programmer bugs (impossible states).70- **MUST** keep `go.mod` tidy: minimal direct dependencies, `go mod tidy` clean, no71 `replace` directives in published modules.72- **SHOULD** use generics for type-safe containers/helpers; do not force generics73 where an interface or concrete type is simpler.74- **SHOULD** prefer `any` over `interface{}` (Go 1.18+), `min`/`max` builtins75 (1.21+), loop-var semantics of 1.22+ (no `v := v` copies), and `for range int`76 (1.22+) for counted loops.77- **SHOULD** use `t.Context()` in tests (Go 1.24+) for cancellation-aware test code.78- **MUST NOT** use `interface{}`/`any` to dodge type safety in exported APIs.79- **MUST NOT** use `math/rand` for security values (`crypto/rand`), or MD5/SHA-1 for80 integrity — see `security`.81- **MUST NOT** ignore errors with `_` in production code paths.82- **MUST NOT** call `os.Exit` or `log.Fatal` inside libraries; return errors and let83 `main` decide.8485## Examples8687Good — idiomatic service call chain (compiles under Go 1.26):8889```go90package orders9192import (93 "context"94 "errors"95 "fmt"96 "log/slog"97 "time"98)99100var ErrOrderNotFound = errors.New("order not found")101102type Order struct {103 ID string104 Status string105}106107// Cancel marks a confirmed order as cancelled; other statuses are rejected.108func (o *Order) Cancel() error {109 if o.Status != "confirmed" {110 return fmt.Errorf("cancel order %s: status %q is not cancellable", o.ID, o.Status)111 }112 o.Status = "cancelled"113 return nil114}115116type Store interface {117 FindOrder(ctx context.Context, id string) (Order, error)118}119120type Service struct {121 store Store122 logger *slog.Logger123}124125func NewService(store Store, logger *slog.Logger) *Service {126 return &Service{store: store, logger: logger}127}128129// Cancel cancels the order with the given id before its cutoff time.130func (s *Service) Cancel(ctx context.Context, id string) error {131 const op = "orders.Cancel"132133 ctx, cancel := context.WithTimeout(ctx, 5*time.Second)134 defer cancel()135136 order, err := s.store.FindOrder(ctx, id)137 switch {138 case errors.Is(err, ErrOrderNotFound):139 return fmt.Errorf("%s: %w", op, err)140 case err != nil:141 return fmt.Errorf("%s: find order: %w", op, err)142 }143144 if err := order.Cancel(); err != nil {145 return fmt.Errorf("%s: cancel order %s: %w", op, id, err)146 }147148 s.logger.InfoContext(ctx, "order cancelled",149 slog.String("order_id", id))150 return nil151}152```153154Bad — the classic violations, all in one function:155156```go157func (s *Service) Cancel(id string) error {158 order, _ := s.store.FindOrder(nil, id) // ignored error, nil context159 if order.ID == "" { // sentinel check by field, not error160 fmt.Println("not found") // fmt instead of slog161 }162 go s.notify(id) // unowned goroutine, race-prone163 return nil164}165```166167Why it fails: swallowed error breaks `errors.Is` handling upstream; `nil` context168panics in store implementations; `fmt.Println` is not structured; the goroutine can169outlive the request and races on shared state.170171## Checklist172173- [ ] `go vet ./...` and `golangci-lint run` clean.174- [ ] `go test -race ./...` green for all packages.175- [ ] All errors wrapped with `%w`; sentinels/typed errors for caller decisions.176- [ ] `context.Context` flows through every I/O call; none stored in long-lived structs.177- [ ] Logging via `slog` with typed attributes; no `fmt.Println`/`log.Printf`.178- [ ] Every goroutine owned and terminable; channels documented sender→receiver.179- [ ] `govulncheck ./...` passes (see `security`).180- [ ] `go mod tidy` leaves no diff.181182## Common Pitfalls183184- **Nil-context calls** — `context.Background()` with a wiring comment is the185 honest stand-in when no caller context exists yet; `nil` never is.186- **Loop-variable paranoia on modern Go** — `v := v` copies are unnecessary since187 Go 1.22; remove them, keep them only in generics weirdness with proof.188- **time.After in loops/selects** — leaks timers until fire; use `time.NewTimer`189 with `Reset` for recurring waits.190- **Error-message capitalization** — Go errors read as sentences mid-flow;191 `"Parse config: ..."` and trailing punctuation are wrong.192- **init() abuse** — implicit wiring hides dependencies; construct explicitly.193194## References195196- Effective Go: https://go.dev/doc/effective_go197- Go documentation — doc comments: https://go.dev/doc/comment198- log/slog package docs: https://pkg.go.dev/log/slog199- Go Code Review Comments (official): https://go.dev/wiki/CodeReviewComments200- Go 1.26 release notes: https://go.dev/doc/go1.26