Go 1.21
This overview applies when the project's target Go version is at least 1.21.
Language
- The built-in
minandmaxfunctions select an extremum from a fixed set of ordered values without hand-written comparisons. clearcan empty a map or zero every element of a slice without deletion or zeroing loops.- Type inference covers more generic function arguments, generic function assignments, and interface method matching, reducing the need for explicit type arguments.
panic(nil)now produces*runtime.PanicNilError, ensuring that a directly deferredrecoverreturns a non-nil value.GODEBUG=panicnil=1provides temporary compatibility with the older behaviour.
Toolchains and builds
- The
godirective ingo.modorgo.workis a strict minimum version, while thetoolchaindirective can suggest a newer toolchain without raising the language and module-semantics minimum. - Automatic toolchain selection can find or download a newer Go release when a module requires it.
- Version-sensitive
GODEBUGdefaults follow thegodirective, allowing a newer toolchain to preserve older language-version behaviour. - A representative production CPU profile named
default.pgoin a main package enables profile-guided optimisation automatically;-pgocontrols an explicit profile location or opt-out. go test -fullpathemits unambiguous source paths, and a directory argument togo test -osupports several test binaries.
Generic collection and logging APIs
- The generic
slicesandmapspackages provide common collection operations that previously required local implementations. cmp.Compare,cmp.Less, andcmp.Orderedsupport ordered generic algorithms and comparators forsliceshelpers.log/slogprovides structured, levelled logging with keys and values orslog.Attrvalues.testing/slogtestvalidates customslog.Handlerimplementations.
Standard library
context.WithoutCancelallows work to outlive parent cancellation while retaining values.WithDeadlineCause,WithTimeoutCause, andCausepreserve deadline reasons, whileAfterFuncregisters cancellation-triggered work.errors.ErrUnsupportedgives unsupported operations a portable identity througherrors.Is.sync.OnceFunc,sync.OnceValue, andsync.OnceValuescover common function and result-caching forms ofsync.Once.reflect.SliceHeaderandreflect.StringHeaderare deprecated;unsafe.Slice,unsafe.SliceData,unsafe.String, andunsafe.StringDataare their safer replacements.- Low-level
crypto/ellipticcurve operations are deprecated.crypto/ecdhcovers ECDH, while lower-level curve arithmetic requires a maintained implementation. runtime.Pinnerallows non-Go code to retain references to explicitly pinned Go memory until it is unpinned.testing.Testingreports whether the running program is ago testbinary.
Experimental and compatibility features
- The per-iteration loop-variable behaviour is a preview in Go 1.21; the language still uses one variable per loop by default.
GOEXPERIMENT=cgocheck2provides the thorough cgo pointer checker, replacing the removed runtime settingGODEBUG=cgocheck=2.- Supported systems start at macOS 10.15, Windows 10, and Windows Server 2016.