Go gives you strong security building blocks: memory safety, a well-maintained cryptography standard library, a TLS stack with safe defaults, context-aware HTML escaping in html/template, and built-in tooling such as govulncheck and the race detector. The language removes whole classes of bugs, but you still have to validate input, use parameterised queries, manage secrets and keep dependencies patched.
This article walks through the features Go provides and the practices to apply in real services, with current, compilable examples.
Go is memory safe in normal code: arrays and slices are bounds checked, there is no pointer arithmetic, and garbage collection prevents use-after-free. Buffer overflows of the C kind do not occur unless you import the unsafe package or call C through cgo, so treat both as high-risk areas and review them carefully.
Static typing and explicit error returns also make mistakes visible. The standard library is maintained by the Go team and receives security fixes through minor releases, so staying on a supported Go version is itself a security control. Run go version in CI and upgrade promptly when a release is marked as a security fix.
Prefer authenticated encryption. AES-GCM encrypts and verifies integrity in one step. The older CFB and CBC modes shown in many tutorials provide no integrity check and should not be used for new code:
Never reuse a nonce with the same key. Use crypto/rand (not math/rand) for keys, tokens and nonces. For passwords, use a slow hash such as argon2id or bcrypt from golang.org/x/crypto, never plain SHA-256, and avoid MD5 and SHA-1 for security purposes. Compare secrets with crypto/subtle.ConstantTimeCompare to prevent timing attacks.
crypto/tls defaults to a modern minimum version, but set it explicitly and configure timeouts, because a default http.Server has none and is exposed to slow-client attacks:
The method-and-path pattern "GET /" requires Go 1.22 or later. MaxBytesHandler limits request body size. Use TLS 1.3 as the minimum when all your clients support it, and TLS 1.2 otherwise. In Kubernetes or behind a proxy, terminate TLS at a component you maintain and keep the headers above.
Validate everything that crosses a trust boundary: length, type, range and allowed characters. Use an allow list rather than trying to strip bad characters.
| Threat | Go defence |
|---|---|
| SQL injection | Parameterised queries with database/sql (db.QueryContext(ctx, "SELECT ... WHERE id = $1", id)), never string concatenation |
| Cross-site scripting | html/template, which escapes by context; never use text/template for HTML |
| Path traversal | os.Root (Go 1.24+) or filepath.Clean plus a prefix check; http.FileServer over http.Dir |
| Command injection | exec.Command("prog", args...) with separate arguments, never sh -c with user data |
| Request forgery | CSRF tokens or SameSite cookies, plus Origin checks |
| Oversized bodies | http.MaxBytesReader and server timeouts |
A safe query looks like this:
And safe HTML rendering:
log/slog with structured fields and redact sensitive ones.rand.Read.context.Context deadlines for outbound calls so a slow dependency cannot exhaust your service.Data races can become security bugs. Protect shared state with sync.Mutex or channels and run tests with go test -race ./.... Use these tools regularly:
govulncheck (from the Go team) reports known vulnerabilities in the dependencies and standard library functions your code actually calls, which makes its output more actionable than a plain version scan. gosec is a third-party static analyser for insecure patterns. Pin dependencies with go.sum, review updates, and rebuild containers when the Go toolchain gets a security release.
CGO_ENABLED=0 and ship them in minimal images such as distroless or scratch.Ordinary Go code is bounds checked and memory safe. The risk returns with the unsafe package and cgo, so isolate and review that code.
Use crypto/rand. The math/rand and math/rand/v2 packages are predictable and meant for simulations and non-security uses.
Run govulncheck ./.... It uses the official Go vulnerability database and only flags vulnerabilities in code paths you call.
No. Use the standard library and golang.org/x/crypto, choose high-level primitives like AES-GCM or x/crypto/chacha20poly1305, and avoid designing custom schemes.
Go's memory safety, standard library cryptography, TLS support and tooling give you a strong base. Use AES-GCM or ChaCha20-Poly1305, crypto/rand, hardened http.Server settings, html/template, parameterised SQL and context deadlines, keep secrets out of code, and run govulncheck, gosec and the race detector in CI while staying on a supported Go release.