← All releases

go1.22.0

Major

February 6, 2024

Go 1.22 changes for-loop semantics so each iteration gets its own variables, adds range-over-integers, and ships math/rand/v2 plus method/wildcard-based net/http routing, alongside PGO devirtualization gains, new vet checks, and a rebuilt execution tracer.

highlights
  • For loops now create a new set of loop variables on each iteration instead of reusing one, eliminating a common class of accidental-capture bugs (vet updated to match for go1.22+ files)

  • "for" loops can now range directly over integers, e.g. "for i := range 10"

  • Preview of range-over-function iterators, enabled via GOEXPERIMENT=rangefunc

  • New math/rand/v2 package with renamed/idiomatic APIs (IntN, Uint64N, etc.), a generic N function, and new ChaCha8/PCG random sources; math/rand's top-level functions now also use ChaCha8 by default

  • net/http.ServeMux patterns now support HTTP methods (e.g. "POST /items/create") and path wildcards (e.g. "/items/{id}", "/files/{path...}"), with new Request.PathValue/SetPathValue methods

  • go.work workspaces can now use a vendor directory via 'go work vendor' and -mod=vendor

  • go test -cover now reports 0.0% coverage for packages without their own tests instead of "[no test files]"

  • vet adds new checks: no-op append calls with no values, non-deferred time.Since inside defer statements, and mismatched key/value pairs in log/slog calls

  • Profile-guided optimization (PGO) now devirtualizes more interface calls, giving roughly 2-14% runtime improvement on representative programs

  • Runtime GC metadata is now stored closer to heap objects, improving CPU performance 1-3% and cutting memory overhead ~1%, but may reduce some object alignment from 16 to 8 bytes, which can break assembly code assuming stricter alignment

  • Execution tracer (runtime/trace) overhauled: uses OS clock, streamable on-the-fly trace partitioning, full syscall durations, OS thread info, and lower tracing overhead; new golang.org/x/exp/trace package for reading traces

  • darwin/amd64 binaries are now PIE by default; Go 1.22 is the last release supporting macOS 10.15 Catalina