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.
Strongly recommended (9–10)¶
-
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 withhtml/template, nottext/template. (specific/go) Automatable: gosec.
Recommended (6–8)¶
-
GO-5(8) Pass the request'scontext.Contextalong the whole call chain, from the incoming request to every outgoing call, as the first parameter, namedctx; never store it in a struct, and use the context-aware APIs, such asQueryContextandhttp.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 async.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 rungovulncheckon 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 anerrorfor 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 async.WaitGroupor anerrgroup.Groupfromgolang.org/x/syncbefore the code that started it returns or shuts down. (specific/go) Automatable: gosec. -
GO-8(7) Create everyhttp.Serveryourself and set its timeouts: alwaysReadHeaderTimeoutandIdleTimeout, andReadTimeoutandWriteTimeouttoo unless handlers set their own deadlines. Don't serve withhttp.ListenAndServeorhttp.Serve, which can't set timeouts. (specific/go) Automatable: gosec. -
GO-9(7) Shut down onSIGTERM: derive the root context fromsignal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM), and when it's done, callServer.Shutdownwith a deadline shorter than the platform's grace period. (specific/go) -
GO-11(7) Commitgo.modandgo.sum, change dependencies only withgo getorgo mod tidyin a reviewed change, and fail CI whengo mod tidy -diffreports a difference. (specific/go) Automatable: go mod tidy -diff. -
GO-14(7) Open onesql.DBfor each database when the program starts and share it, because it is a connection pool that is safe for concurrent use, and close everysql.Rowswithdefer rows.Close()and checkrows.Err(). (specific/go) -
GO-15(7) In CI, fail the build on unformatted code (gofmt -l), on anygo 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 incmd/<name>/, the packages that implement them underinternal/, and each package named for what it provides, notutil,commonorhelper. (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 withlog/slog: in a service, set a JSON handler on stdout as the default logger, withslog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil))), call the...Contextlogging methods so that handlers can add trace context, and give types that hold secrets aLogValuemethod 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, withgo test -fuzz, and test code with timers, timeouts or background goroutines insidesynctest.Testinstead of waiting in real time. (specific/go)