Skip to content

Go checklist

Copy this list into a pull request, design review or project kickoff and tick what applies. Rules are grouped by level and sorted by score (criteria). Each links to the file that explains it and cites its sources. Automatable names tools that can check the rule in CI (see the file's "How to enforce" section).

Also apply the general checklist.

This file is generated by python3 scripts/rank.py fix from the guidelines. Don't edit it by hand.

  • GO-13 (9) Never build SQL statements or HTML by formatting or concatenating strings with values: pass values to SQL as arguments for placeholders (? or $1), and render HTML with html/template, not text/template. (specific/go) Automatable: gosec.
  • GO-5 (8) Pass the request's context.Context along the whole call chain, from the incoming request to every outgoing call, as the first parameter, named ctx; never store it in a struct, and use the context-aware APIs, such as QueryContext and http.NewRequestWithContext. (specific/go) Automatable: go vet, golangci-lint, gosec, Staticcheck checks.
  • GO-7 (8) Never share a variable between goroutines without synchronization: hand data over on channels, or guard shared state, such as a cache, with a sync.Mutex, and run the tests with the race detector (go test -race) in CI. (specific/go) Automatable: Data Race Detector.
  • GO-12 (8) Build with a supported Go release, one of the two newest major versions, at its latest minor version, and run govulncheck on every change in CI and regularly on the code or binaries you have deployed. (specific/go) Automatable: govulncheck exit codes.
  • GO-3 (7) Check every error, then either handle it or return it with added context, not both: don't discard errors with _, and don't log an error that you return. (specific/go) Automatable: go vet, golangci-lint.
  • GO-4 (7) Return an error for every failure a caller can expect, such as invalid input, failed I/O or bad configuration, and panic only for bugs that tests and reviews should catch, such as API misuse or a broken invariant. In a server, recover at the top of each goroutine that serves one connection, job or match. (specific/go)
  • GO-6 (7) Give each goroutine you start a context to stop on, and wait for it with a sync.WaitGroup or an errgroup.Group from golang.org/x/sync before the code that started it returns or shuts down. (specific/go) Automatable: gosec.
  • GO-8 (7) Create every http.Server yourself and set its timeouts: always ReadHeaderTimeout and IdleTimeout, and ReadTimeout and WriteTimeout too unless handlers set their own deadlines. Don't serve with http.ListenAndServe or http.Serve, which can't set timeouts. (specific/go) Automatable: gosec.
  • GO-9 (7) Shut down on SIGTERM: derive the root context from signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM), and when it's done, call Server.Shutdown with a deadline shorter than the platform's grace period. (specific/go)
  • GO-11 (7) Commit go.mod and go.sum, change dependencies only with go get or go mod tidy in a reviewed change, and fail CI when go mod tidy -diff reports a difference. (specific/go) Automatable: go mod tidy -diff.
  • GO-14 (7) Open one sql.DB for each database when the program starts and share it, because it is a connection pool that is safe for concurrent use, and close every sql.Rows with defer rows.Close() and check rows.Err(). (specific/go)
  • GO-15 (7) In CI, fail the build on unformatted code (gofmt -l), on any go vet ./... finding and on linter findings, for example from Staticcheck or golangci-lint. (specific/go) Automatable: go vet, gofmt, golangci-lint, Staticcheck checks.
  • GO-1 (6) Lay out a service as "Organizing a Go module" describes for a server project: each command in cmd/<name>/, the packages that implement them under internal/, and each package named for what it provides, not util, common or helper. (specific/go)
  • GO-2 (6) Define interfaces in the package that uses them, keep them small, and return concrete types; don't add an interface before a consumer needs it, and don't export one from the implementing package only so that it can be mocked. (specific/go)
  • GO-10 (6) Log with log/slog: in a service, set a JSON handler on stdout as the default logger, with slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil))), call the ...Context logging methods so that handlers can add trace context, and give types that hold secrets a LogValue method that redacts them. (specific/go) Automatable: golangci-lint.
  • GO-16 (6) Fuzz the code that parses untrusted input, such as request decoders and network protocol handlers, with go test -fuzz, and test code with timers, timeouts or background goroutines inside synctest.Test instead of waiting in real time. (specific/go)