# Go 1 21

> Apply Go 1.21 language features, tooling, standard-library APIs, migrations, and behaviour changes. Load for Go work targeting 1.21 or later when the model's knowledge may predate the 2023-08-08 release.

- Skill: `cedws/go-1-21` (Agent Skill)
- Install (CLI): `npx skillmds@latest add cedws/go-1-21`
- Raw SKILL.md: https://api.skillmd.com/api/skills/cedws/go-1-21/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- Author: cedws (https://skillmd.com/u/cedws)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/cedws/go-1-21

---


# Go 1.21

This overview applies when the project's target Go version is at least 1.21.

## Language

- The built-in `min` and `max` functions select an extremum from a fixed set of ordered values without hand-written comparisons.
- `clear` can 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 deferred `recover` returns a non-nil value. `GODEBUG=panicnil=1` provides temporary compatibility with the older behaviour.

## Toolchains and builds

- The `go` directive in `go.mod` or `go.work` is a strict minimum version, while the `toolchain` directive 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 `GODEBUG` defaults follow the `go` directive, allowing a newer toolchain to preserve older language-version behaviour.
- A representative production CPU profile named `default.pgo` in a main package enables profile-guided optimisation automatically; `-pgo` controls an explicit profile location or opt-out.
- `go test -fullpath` emits unambiguous source paths, and a directory argument to `go test -o` supports several test binaries.

## Generic collection and logging APIs

- The generic `slices` and `maps` packages provide common collection operations that previously required local implementations.
- `cmp.Compare`, `cmp.Less`, and `cmp.Ordered` support ordered generic algorithms and comparators for `slices` helpers.
- `log/slog` provides structured, levelled logging with keys and values or `slog.Attr` values.
- `testing/slogtest` validates custom `slog.Handler` implementations.

## Standard library

- `context.WithoutCancel` allows work to outlive parent cancellation while retaining values. `WithDeadlineCause`, `WithTimeoutCause`, and `Cause` preserve deadline reasons, while `AfterFunc` registers cancellation-triggered work.
- `errors.ErrUnsupported` gives unsupported operations a portable identity through `errors.Is`.
- `sync.OnceFunc`, `sync.OnceValue`, and `sync.OnceValues` cover common function and result-caching forms of `sync.Once`.
- `reflect.SliceHeader` and `reflect.StringHeader` are deprecated; `unsafe.Slice`, `unsafe.SliceData`, `unsafe.String`, and `unsafe.StringData` are their safer replacements.
- Low-level `crypto/elliptic` curve operations are deprecated. `crypto/ecdh` covers ECDH, while lower-level curve arithmetic requires a maintained implementation.
- `runtime.Pinner` allows non-Go code to retain references to explicitly pinned Go memory until it is unpinned.
- `testing.Testing` reports whether the running program is a `go test` binary.

## 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=cgocheck2` provides the thorough cgo pointer checker, replacing the removed runtime setting `GODEBUG=cgocheck=2`.
- Supported systems start at macOS 10.15, Windows 10, and Windows Server 2016.

