Skip to content

Go

Scope: services, command-line tools and game servers written in Go, on the two releases the Go team supports: Go 1.27 (August 2026) and Go 1.26 (February 2026). Go 1.25 stopped receiving fixes when Go 1.27 came out. For configuration, health checks, telemetry and resilience, follow general/operations-reliability.md; for secrets, authorization and the supply chain, general/security.md; for test strategy and CI, the TEST rules in general/testing-delivery.md; for container images, Docker; for databases, PostgreSQL; and for hosting, scaling and securing game servers, Game servers and online services. The rules here cover what is specific to Go.

Entries are sorted by score from 1 to 10 (criteria): 9–10 start here or strongly recommended, 6–8 recommended, 1–5 further reading or conditional.

Adopted references

  • 8 Go Style (Google): the baseline for code style: the canonical Style Guide, the normative Style Decisions, and Best Practices for naming, package size, errors, interfaces, concurrency, contexts, global state and tests. A few points refer to Google's internal libraries, such as its own log package.
  • 8 Go Code Review Comments (Go wiki): the comments most often made in Go code reviews, such as contexts, goroutine lifetimes, error handling, interfaces and package names, each with a short name to cite in a review. Go Test Comments adds those for tests.
  • 8 Organizing a Go module (Go): the official layouts for a package, a command, a module with several commands, and a server project, with internal/ and cmd/ directories.
  • 8 Go Modules Reference (Go): go.mod and go.sum, minimal version selection, module proxies, the checksum database and private modules.
  • 7 Security Best Practices for Go Developers (Go): scanning for known vulnerabilities with govulncheck, keeping Go and dependencies up to date, fuzzing, the race detector and go vet.
  • 7 Effective Go (Go): idiomatic use of the core language, including interfaces, concurrency, errors, panic and recover. It was written for Go's release in 2009 and isn't actively updated, so it doesn't cover generics, modules or newer libraries.
  • 7 Release History (Go): the release policy, under which each major release is supported until there are two newer major releases, and what each minor release fixed.
  • 7 Accessing relational databases (Go): the database/sql guides to opening a handle, querying, transactions, cancellation, the connection pool and avoiding SQL injection.
  • 7 A Guide to the Go Garbage Collector (Go): how GOGC and the memory limit (GOMEMLIMIT) trade memory for CPU time. It advises a memory limit, with 5–10% headroom, for a service in a container with a fixed memory limit, and none for a command-line tool whose memory use depends on its input.
  • 6 Go Runtime (Heroic Labs, Nakama): writing Nakama server modules in Go. The plugin must be built for the same platform, with the same Go version and the same versions of shared dependencies as the Nakama binary; Dependency Pinning explains how.

Key rules

Code structure

Errors

Context and concurrency

HTTP servers

  • 7 GO-8 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. A zero timeout means no timeout, so a slow client can hold a connection and its goroutine as long as it likes. Handlers that need longer, such as uploads or streams, can extend their deadlines with http.ResponseController; limit request bodies with http.MaxBytesReader. This applies API-11. (net/http, Server, net/http, ResponseController, net/http, MaxBytesReader, gosec, G112 and G114)
  • 7 GO-9 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. By default a SIGTERM makes a Go program exit. Shutdown stops listening and waits for active connections to go idle, but not for hijacked connections such as WebSockets: tell those to close from a function registered with RegisterOnShutdown. This applies OPS-15 and K8S-3. (os/signal, Default behavior of signals, os/signal, NotifyContext, net/http, Server.Shutdown, net/http, Server.RegisterOnShutdown)

Logging

Dependencies and toolchain

Data and output

  • 9 GO-13 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. Convert a value to template.HTML or another of its trusted types only if it comes from a trusted source or a sound HTML sanitizer, because it is inserted without escaping. This applies SEC-18 and WEB-8. (Go, Avoiding SQL injection risk, html/template, html/template, HTML)
  • 7 GO-14 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(). By default the pool opens a new connection whenever all are busy, with no limit: if all your instances together could open more connections than the database accepts, cap them with SetMaxOpenConns, so that callers wait for a free connection, or add a pooler (PG-9). (Go, database/sql Open, Go, Managing connections, Go, Querying for data: multiple rows)

Testing and code quality

  • 7 GO-15 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. go test runs only a high-confidence subset of the vet checks, which leaves out checks such as lostcancel and copylocks, so run go vet on its own. (Go Code Review Comments, Gofmt, Go Style Guide, Formatting, go command, Test packages, Go, cmd/vet, Staticcheck)
  • 6 GO-16 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. Commit the failing inputs that fuzzing saves under testdata/fuzz, which go test then runs as regression tests. synctest.Test runs the test on a fake clock, waits for its goroutines to exit, and fails if they deadlock. (Go Security Best Practices, Fuzzing, Go Fuzzing, testing/synctest)

How to enforce

Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.

  • 7 go vet: go vet ./... exits with a non-zero status when it reports a problem. Its lostcancel check reports cancel functions that aren't called on every path, copylocks reports locks copied by value, errorsas reports wrong arguments to errors.As, and from Go 1.27 printf reports %w operands that are pointers to an error type. Checks GO-15, and partly GO-3 and GO-5.
  • 7 Data Race Detector: go test -race reports the data races that happen while the tests run, with the stacks of the conflicting accesses, and exits with a non-zero status (66 by default). Partly checks GO-7.
  • 7 go mod tidy -diff: prints the changes that go.mod and go.sum need without making them, and exits with a non-zero status if there are any; go mod verify fails when a downloaded module in the cache has changed. Partly checks GO-11.
  • 6 gofmt: gofmt -l . lists the files whose formatting differs from gofmt's; fail CI when it lists any. Partly checks GO-15.
  • 6 govulncheck exit codes: govulncheck ./... exits with a non-zero status when it finds known vulnerabilities, in the standard library or in dependencies, that affect the code, unless you ask for JSON, SARIF or OpenVEX output. It analyzes source code with the Go version on the PATH, and a binary with the version that built it, so it covers that release's standard library too. Partly checks GO-12.
  • 6 golangci-lint: runs many linters in one pass, with errcheck, govet, ineffassign, staticcheck and unused on by default; errcheck reports unchecked errors. Also enable errorlint with its comparison and asserts checks, for errors compared with == or type assertions, containedctx for contexts stored in structs, noctx for HTTP, network and database calls made without a context, and sloglint with context: all for slog calls without a context. Checks GO-15, and partly GO-3, GO-5 and GO-10.
  • 6 gosec: G112 reports an http.Server without ReadHeaderTimeout, G114 the net/http serve functions that can't set timeouts, G201 and G202 SQL built with format strings or concatenation, G203 unescaped data in HTML templates, G701 SQL injection found by taint analysis, and G118 unused cancel functions, goroutines that use context.Background() where a request context exists, and I/O loops that never check ctx.Done(). It exits with status 1 on findings, and also runs inside golangci-lint. Partly checks GO-5, GO-6, GO-8 and GO-13.
  • 6 Staticcheck checks: more than 150 checks for bugs, performance problems and style, such as SA1012 for a nil context.Context. Run staticcheck ./... in CI, or through golangci-lint, which enables it by default. Checks GO-15, and partly GO-5.

Tutorials and further reading