For years, working on several Go projects meant a version manager: gvm, goenv, asdf, or a hand-rolled set of directories under /usr/local. Go 1.21 changed that. The go command itself can now read the version a module needs, download that toolchain and run it, and the setting lives in go.mod where everyone already looks.
We migrated a monorepo of about forty Go services from gvm to toolchain switching. This guide explains the mechanism, the settings that matter in CI and restricted networks, and the cases where gvm or Go's own wrapper commands are still useful.
Two lines in go.mod
Since Go 1.21, go.mod has two related lines:
module example.com/billing
go 1.23.0
toolchain go1.23.4
Line | Meaning | Enforced? |
|---|---|---|
| Minimum Go version required to build this module, and the language version for its code | Yes, strictly since 1.21 |
| Preferred toolchain when running | Only as a preference, never lower than |
Before 1.21 the go line was advisory and older compilers tried anyway. Since 1.21 it is a requirement: a Go 1.22 binary will not build a module that says go 1.23.0 unless it can switch to a newer toolchain.
The toolchain line is only written when it differs from what the go line implies, and only the main module's toolchain line counts; dependencies' toolchain lines are ignored.
How switching works
The go binary on your PATH is now a launcher as much as a compiler. When you run any go command in a module, it compares its own version with what the module requires and with the GOTOOLCHAIN setting, then either runs itself or downloads and runs another version.
Downloaded toolchains are ordinary modules (golang.org/toolchain at versions like v0.0.1-go1.23.4.linux-amd64), fetched through GOPROXY and verified against the checksum database like any dependency. They land in the module cache, so the second run is instant.
go version # the local binary, e.g. go1.22.6
cd services/billing
go version # go1.23.4 if go.mod asks for it
go env GOTOOLCHAIN
GOTOOLCHAIN values
GOTOOLCHAIN controls this behaviour. You can set it as an environment variable or persist it with go env -w.
Value | Behaviour |
|---|---|
| Use the local toolchain unless the module needs a newer one, then download it |
| Always use the local binary; fail if the module needs newer |
| Always use exactly that version, downloading if needed |
| Use at least that version, newer if the module requires it |
| Like |
With GOTOOLCHAIN=local, an old binary in a newer module fails with a clear message:
go: go.mod requires go >= 1.23.0 (running go 1.22.6; GOTOOLCHAIN=local)
That message is the single most common question we got during the migration. It is the system working: either upgrade the local Go, or allow switching.
Changing versions in a module
Use the go command to edit the lines rather than editing by hand, because it also updates requirements consistently:
go get go@1.23.0 # raise the minimum
go get toolchain@go1.23.4 # set the preferred toolchain
go mod edit -toolchain=none # drop the toolchain line
Raising the go line is a breaking change for anyone depending on your module, since their builds now require that version. For libraries, keep the go line as low as your code allows and use toolchain for what you build and test with. For applications, both can follow your release cadence.
go get on a dependency that requires a newer go version also raises your go line. Read the diff of go.mod in every dependency update for that reason.
CI settings
In CI you want reproducible builds and predictable downloads.
Let the module decide. Install any recent Go and leave
GOTOOLCHAINatauto. The job uses exactly whatgo.modsays. Cache the module cache directory so toolchain downloads happen once.Or install the exact version. Setup actions such as
actions/setup-goacceptgo-version-file: go.mod. Pair this withGOTOOLCHAIN=localso a mismatch fails the job instead of silently downloading.
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- run: go version
env:
GOTOOLCHAIN: local
- run: go test ./...
env:
GOTOOLCHAIN: local
We chose the second for release builds because the build image is scanned and signed, and we did not want a different compiler fetched at build time. Developer laptops stay on auto.
Restricted networks
If GOPROXY points at an internal proxy, toolchain modules must be available there too. Most module proxies cache golang.org/toolchain like any module, but check yours. In fully offline environments set GOTOOLCHAIN=local or path and install toolchains through your normal artifact process. GOTOOLCHAIN=path is handy when you pre-install versions as go1.22.6 and go1.23.4 binaries on PATH.
When gvm still helps
gvm installs Go versions under ~/.gvm and switches by editing PATH, GOROOT and optionally GOPATH:
gvm install go1.20.14 -B # -B uses a binary release instead of building
gvm use go1.20.14 --default
gvm list
It still earns its place in a few situations:
Situation | Why toolchain switching is not enough |
|---|---|
Modules whose | Switching only goes up; an old project might break on a new compiler's vet or behaviour changes |
Pre-modules GOPATH projects | gvm's pkgsets give each project its own |
Testing your code on many old Go versions locally | Easier to flip a shell between versions |
Teams on mixed shells with existing gvm scripts | Migration cost may not be worth it yet |
For the testing case there is also Go's own lightweight wrapper, which needs no extra tool:
go install golang.org/dl/go1.20.14@latest
go1.20.14 download
go1.20.14 test ./...
Each wrapper binary installs its SDK under ~/sdk/ and runs it on demand. For most teams that covers what gvm used to be needed for.
Our migration steps
Installed one recent Go on laptops (Homebrew or the official installer), removed gvm's lines from shell startup files.
Ran
go get toolchain@go1.23.4in each service and committed thego.modchanges.Switched release builds to
go-version-fileplusGOTOOLCHAIN=local.Confirmed the internal module proxy served
golang.org/toolchain.Kept two legacy services on gvm until their
goline was raised past 1.21.
Common mistakes
Mistake | Why it hurts | Do this instead |
|---|---|---|
Bumping the | Forces every consumer onto that version | Keep |
| Every newer module fails to build | Use |
Ignoring |
| Review |
Internal proxy without toolchain modules | Downloads fail with not found errors | Allow |
Keeping gvm's | Conflicts with the switched toolchain | Remove gvm init from shell files |
Key takeaways
Since Go 1.21,
goingo.modis a hard minimum andtoolchainis a preference.The
gocommand downloads and runs the right toolchain whenGOTOOLCHAINallows it.Use
autofor development,localwith an exact install for signed release builds.Toolchains come through
GOPROXYand the checksum database, so mirror them if you run a proxy.Keep gvm or
golang.org/dlwrappers for pre-1.21 modules and multi-version testing.
References
Go toolchains documentation: https://go.dev/doc/toolchain
go.mod reference (
goandtoolchaindirectives): https://go.dev/doc/modules/gomod-refGo 1.21 release notes: https://go.dev/doc/go1.21
Managing Go installations: https://go.dev/doc/manage-install
Go module proxy and checksum database: https://proxy.golang.org/
actions/setup-go: https://github.com/actions/setup-go
gvm README: https://github.com/moovweb/gvm
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.