Go 1.22
This overview applies when the project's target Go version is at least 1.22.
Language
- Loop variables declared by a
for loop are distinct for every iteration, making defensive shadow assignments for closures and goroutines unnecessary.
- Integer ranges iterate from zero to one less than a bound.
for index := range count {
process(index)
}
GOEXPERIMENT=rangefunc enables a preview of range-over-function iterators; it is not a stable Go 1.22 language feature.
Tools and workspaces
go work vendor creates a shared vendor directory for a multi-module workspace; that directory cannot simultaneously represent single-module vendoring state.
go get no longer works in legacy GOPATH mode, so dependency changes occur inside modules.
go test -cover reports zero coverage for packages containing executable code but no tests.
go vet detects ineffective append calls, prematurely evaluated time.Since calls in defer, and malformed alternating key-value arguments to log/slog.
- The refreshed execution tracer supports streamable traces, complete system-call durations, and correlation with operating-system clocks.
GOEXPERIMENT=noexectracer2 restores the older tracer temporarily.
Randomness and versions
math/rand/v2 is the modern non-cryptographic random-number API, with IntN, UintN, generic N, and the PCG and ChaCha8 sources for local state.
crypto/rand.Read remains the API for random bytes that protect secrets; math/rand/v2 is not a substitute.
go/version validates and compares Go version strings without ad hoc parsing.
HTTP routing and filesystems
http.ServeMux patterns can include HTTP methods and wildcards, with wildcard values available through Request.PathValue.
mux.HandleFunc("GET /items/{id}", func(writer http.ResponseWriter, request *http.Request) {
id := request.PathValue("id")
serveItem(writer, request, id)
})
- Braces, escaped paths, and conflicting patterns can behave differently under the new routing semantics;
GODEBUG=httpmuxgo121=1 temporarily restores the old behaviour.
http.ServeFileFS, http.FileServerFS, and http.NewFileTransportFS serve content from an fs.FS.
archive/tar.Writer.AddFS and archive/zip.Writer.AddFS add an fs.FS without a manual walk.
Standard library and runtime
cmp.Or selects the first non-zero value in an ordered fallback sequence.
slices.Concat concatenates multiple slices. Delete, DeleteFunc, Compact, CompactFunc, and Replace now zero obsolete elements.
reflect.TypeFor[T]() replaces the reflect.TypeOf((*T)(nil)).Elem() idiom, and reflect.PointerTo replaces deprecated reflect.PtrTo.
/sched/pauses/total/gc:seconds replaces the deprecated /gc/pauses:seconds metric, with additional histograms separating stop coordination from complete pauses.
go/types can expose alias nodes under GODEBUG=gotypesalias=1, so named-looking types are not necessarily *types.Named.
Security and compatibility
- TLS servers with no explicit minimum require TLS 1.2, and pre-TLS 1.3 defaults exclude non-ECDHE cipher suites.
tls10server=1 and tlsrsakex=1 provide temporary legacy compatibility.
CertPool.AddCertWithConstraint associates additional chain-building checks with a custom trust root.
slices.Insert panics for every out-of-range insertion index, including when no values are inserted.
1---2name: go-1-223description: Apply Go 1.22 language features, tooling, standard-library APIs, migrations, and behaviour changes. Load for Go work targeting 1.22 or later when the model's knowledge may predate the 2024-02-06 release.4---56# Go 1.2278This overview applies when the project's target Go version is at least 1.22.910## Language1112- Loop variables declared by a `for` loop are distinct for every iteration, making defensive shadow assignments for closures and goroutines unnecessary.13- Integer ranges iterate from zero to one less than a bound.1415```go16for index := range count {17 process(index)18}19```2021- `GOEXPERIMENT=rangefunc` enables a preview of range-over-function iterators; it is not a stable Go 1.22 language feature.2223## Tools and workspaces2425- `go work vendor` creates a shared vendor directory for a multi-module workspace; that directory cannot simultaneously represent single-module vendoring state.26- `go get` no longer works in legacy `GOPATH` mode, so dependency changes occur inside modules.27- `go test -cover` reports zero coverage for packages containing executable code but no tests.28- `go vet` detects ineffective `append` calls, prematurely evaluated `time.Since` calls in `defer`, and malformed alternating key-value arguments to `log/slog`.29- The refreshed execution tracer supports streamable traces, complete system-call durations, and correlation with operating-system clocks. `GOEXPERIMENT=noexectracer2` restores the older tracer temporarily.3031## Randomness and versions3233- `math/rand/v2` is the modern non-cryptographic random-number API, with `IntN`, `UintN`, generic `N`, and the PCG and ChaCha8 sources for local state.34- `crypto/rand.Read` remains the API for random bytes that protect secrets; `math/rand/v2` is not a substitute.35- `go/version` validates and compares Go version strings without ad hoc parsing.3637## HTTP routing and filesystems3839- `http.ServeMux` patterns can include HTTP methods and wildcards, with wildcard values available through `Request.PathValue`.4041```go42mux.HandleFunc("GET /items/{id}", func(writer http.ResponseWriter, request *http.Request) {43 id := request.PathValue("id")44 serveItem(writer, request, id)45})46```4748- Braces, escaped paths, and conflicting patterns can behave differently under the new routing semantics; `GODEBUG=httpmuxgo121=1` temporarily restores the old behaviour.49- `http.ServeFileFS`, `http.FileServerFS`, and `http.NewFileTransportFS` serve content from an `fs.FS`.50- `archive/tar.Writer.AddFS` and `archive/zip.Writer.AddFS` add an `fs.FS` without a manual walk.5152## Standard library and runtime5354- `cmp.Or` selects the first non-zero value in an ordered fallback sequence.55- `slices.Concat` concatenates multiple slices. `Delete`, `DeleteFunc`, `Compact`, `CompactFunc`, and `Replace` now zero obsolete elements.56- `reflect.TypeFor[T]()` replaces the `reflect.TypeOf((*T)(nil)).Elem()` idiom, and `reflect.PointerTo` replaces deprecated `reflect.PtrTo`.57- `/sched/pauses/total/gc:seconds` replaces the deprecated `/gc/pauses:seconds` metric, with additional histograms separating stop coordination from complete pauses.58- `go/types` can expose alias nodes under `GODEBUG=gotypesalias=1`, so named-looking types are not necessarily `*types.Named`.5960## Security and compatibility6162- TLS servers with no explicit minimum require TLS 1.2, and pre-TLS 1.3 defaults exclude non-ECDHE cipher suites. `tls10server=1` and `tlsrsakex=1` provide temporary legacy compatibility.63- `CertPool.AddCertWithConstraint` associates additional chain-building checks with a custom trust root.64- `slices.Insert` panics for every out-of-range insertion index, including when no values are inserted.