Go versions and toolchains
suggest changeSince Go 1.21 the Go version a module needs is part of the module itself. go.mod says which language version the code is written against and, optionally, which exact toolchain should build it. The go command reads that information and, if needed, downloads and runs a different version of itself.
This article covers:
- what the
goline ingo.modmeans - the
toolchainline and automatic toolchain downloads - the
GOTOOLCHAINenvironment variable - changing versions with
go get go@...andgo get toolchain@... - GODEBUG compatibility settings, the
godebugblock ingo.modand//go:debugdirectives - the
stdversionvet check that catches use of too-new standard library APIs
For where Go itself and your downloaded modules live, see GOPATH, GOROOT, GOBIN and go env.
The go line
go mod init writes a go line with the version of the toolchain that ran it:
$ go version
go version go1.27.0 windows/amd64
$ go mod init example.com/hello
go: creating new go.mod: module example.com/hello
$ cat go.mod
module example.com/hello
go 1.27.0
Since Go 1.21 the go line is a minimum requirement, not a hint. It means "this module needs at least Go 1.27.0". It has three effects:
- Older toolchains refuse to build the module (or switch to a newer one, see below).
- It selects the language version. The compiler enables or disables language features and changes semantics based on it.
- It selects GODEBUG defaults for standard library behaviors that changed over time (covered later).
A version can be a release like 1.27.0, a language version like 1.27 (older go.mod files often have that), or a pre-release like 1.28rc1. 1.27 and 1.27.0 are not the same thing: go 1.27 is satisfied by the go1.27rc1 pre-release, go 1.27.0 is not.
The language version changes how code compiles
A go 1.21 module can't use features added in Go 1.22, even when you build it with Go 1.27:
package main
import "fmt"
func main() {
for i := range 3 {
fmt.Println(i)
}
}
With go 1.21 in go.mod:
$ go build .
# example.com/langv
.\main.go:6:17: cannot range over 3 (untyped int constant): requires go1.22 or later (-lang was set to go1.21; check go.mod)
A file can raise its own language version with a build constraint. //go:build go1.22 means "only compile this file with Go 1.22 or later", so inside that file the 1.22 features are allowed:
//go:build go1.22
package main
import "fmt"
func main() {
for i := range 3 {
fmt.Println(i)
}
}
$ go run .
0
1
2
The best-known case where the version changes meaning rather than what's allowed is the loop variable change from Go 1.22. This program prints different output depending only on the go line:
package main
import "fmt"
func main() {
var prints []func()
for i := 0; i < 3; i++ {
prints = append(prints, func() { fmt.Print(i, " ") })
}
for _, p := range prints {
p()
}
fmt.Println()
}
$ cat go.mod
module example.com/loopv
go 1.21
$ go run .
3 3 3
$ go mod edit -go=1.22
$ go run .
0 1 2
Before 1.22 all closures share one i. From 1.22 each iteration has its own. See Common pitfalls for more on this.
The language version is per module: your dependencies are compiled with the version from their go.mod, so bumping your go line never changes how a dependency's code behaves.
Dependencies raise your minimum
Your module's go line must be at least as high as the go line of every dependency. If you depend on a module that says go 1.26.0, your module needs Go 1.26.0 too. The go command enforces it:
$ cat go.mod
module example.com/app
go 1.24.0
require example.com/dep v0.0.0
replace example.com/dep => ../dep
$ cat ../dep/go.mod
module example.com/dep
go 1.26.0
$ go build .
go: updates to go.mod needed; to update it:
go mod tidy
$ go mod tidy
$ grep "^go " go.mod
go 1.26.0
go get does the same when you add or upgrade a dependency that needs a newer Go.
If you write a library, keep the go line as low as you can reasonably support. Every user of your library inherits it as a minimum.
Forward compatibility: when the toolchain is too old
What happens when go.mod asks for a newer Go than the one installed? Set GOTOOLCHAIN=local to see what plain Go 1.27.0 does on its own:
$ cat go.mod
module example.com/future
go 1.27.2
$ GOTOOLCHAIN=local go build .
go: go.mod requires go >= 1.27.2 (running go 1.27.0; GOTOOLCHAIN=local)
Go 1.21 and later refuse to build code that needs a newer Go. Before 1.21 the go line was only advisory: an old toolchain would try anyway, and fail with confusing compile errors if the code used newer features.
With the default GOTOOLCHAIN=auto, the go command doesn't give up. It downloads the required toolchain and re-runs itself with it:
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Println("built with", runtime.Version())
}
$ go run .
go: downloading go1.27.2 (windows/amd64)
built with go1.27.2
$ go version
go version go1.27.2 windows/amd64
Notes:
- toolchains are downloaded as modules (
golang.org/toolchain) throughGOPROXYand verified against the checksum database, just like any other module - they're stored in the module cache, so the download happens once. Here it ended up in
$GOMODCACHE/golang.org/toolchain@v0.0.1-go1.27.2.windows-amd64.go clean -modcachedeletes them along with everything else - inside the module,
go versionandgo env GOROOTreport the toolchain that will actually be used - a
go 1.28line means "Go 1.28.0 or later", so if that release doesn't exist yet, the download fails:
$ cat go.mod
module example.com/future
go 1.28
$ go run .
go: downloading go1.28.0 (windows/amd64)
go: download go1.28.0 for windows/amd64: toolchain not available
The toolchain line
The go line says the minimum. The optional toolchain line says which toolchain you prefer when working in this module:
$ go mod edit -toolchain=go1.27.2
$ cat go.mod
module example.com/hello
go 1.27.0
toolchain go1.27.2
$ go run .
built with go1.27.2
Users of your module still only need Go 1.27.0. The toolchain line is honored only when this module is the main module (the one you run go commands in). It's ignored when the module is a dependency.
Typical uses:
- the code works with 1.27.0, but the project wants everyone on the team (and CI) to build with 1.27.2 because of a compiler or security fix
- a library keeps a low
goline for its users but develops with a recent toolchain
The toolchain line only ever upgrades. If your installed Go is newer than the toolchain line, the installed one is used. A toolchain value equal to the go line is redundant, and go get removes it.
GOTOOLCHAIN
GOTOOLCHAIN controls which toolchain the go command runs. The default comes from $GOROOT/go.env, which ships with Go:
$ cat "$(go env GOROOT)/go.env"
...
# Automatically download newer toolchains as directed by go.mod files.
# See https://go.dev/doc/toolchain for details.
GOTOOLCHAIN=auto
The possible values:
| Value | Meaning |
|---|---|
auto |
same as local+auto: use the installed Go, switch to a newer one if go.mod asks for it (default) |
local |
always use the installed Go, fail if go.mod requires a newer one |
path |
same as local+path: switch if needed, but only to a goX.Y.Z binary found in PATH, never download |
go1.26.3 |
always use exactly this version (downloading it if needed), ignore go.mod |
go1.27.0+auto |
use at least go1.27.0, or what go.mod asks for, if higher |
go1.27.0+path |
like +auto, without downloads |
GOTOOLCHAIN=local is the setting for environments that must not download anything, like reproducible builds or air-gapped machines. With path, it looks for binaries named like go1.27.2 on PATH:
$ GOTOOLCHAIN=path go version
go: cannot find "go1.27.2" in PATH
A specific version is handy for checking that your code still builds with an older Go:
$ cat go.mod
module example.com/upg
go 1.24.0
$ GOTOOLCHAIN=go1.26.3 go run .
go: downloading go1.26.3 (windows/amd64)
built with go1.26.3
A fixed version doesn't override the go line: if the module needs something newer, you get an error:
$ GOTOOLCHAIN=go1.26.3 go run .
go: go.mod requires go >= 1.27.0 (running go 1.26.3; GOTOOLCHAIN=go1.26.3)
You can set it permanently for your user with go env -w GOTOOLCHAIN=local, but for most people auto is the right choice. In CI, setting GOTOOLCHAIN=local makes sure the job really tests the Go version the CI installed, instead of silently downloading another one.
Before Go 1.21 the usual way to have several Go versions was golang.org/dl: go install golang.org/dl/go1.26.3@latest installs a go1.26.3 wrapper command, and go1.26.3 download fetches that release into ~/sdk. That still works and combines well with GOTOOLCHAIN=path, but for most purposes GOTOOLCHAIN=go1.26.3 is simpler.
Changing versions with go get
Treat go and toolchain like dependencies and use go get to change them. Unlike go mod edit, go get checks requirements and switches toolchains as needed.
$ cat go.mod
module example.com/upg
go 1.24.0
$ go get go@1.25
go: upgraded go 1.24.0 => 1.25.14
go@1.25 means "the latest 1.25 release". To get an exact version, spell it out. When the new minimum is newer than the running toolchain, go get switches first:
$ go get go@1.27.2
go: updating go.mod requires go >= 1.27.2; switching to go1.27.2
go: upgraded go 1.25.14 => 1.27.2
Other useful forms:
# set both lines at once
$ go get go@1.25.0 toolchain@go1.27.2
go: upgraded go 1.24.0 => 1.25.0
go: added toolchain go1.27.2
$ cat go.mod
module example.com/upg
go 1.25.0
toolchain go1.27.2
# remove the toolchain line
$ go get toolchain@none
# move to the latest Go release
$ go get go@latest
go mod edit -go=1.23 and go mod edit -toolchain=go1.27.2 only rewrite the file without any checks, which is fine for scripts but can leave go.mod inconsistent with your dependencies.
GODEBUG: keeping old behavior
Sometimes the standard library has to change behavior that existing programs may depend on. Go handles this with GODEBUG settings: each such change gets a named setting that restores the old behavior, and the default is chosen by the go line of the main module.
Here's an example: since Go 1.21 panic(nil) panics with a *runtime.PanicNilError, so recover() returns a non-nil value. Before 1.21 recover() returned nil, and you couldn't tell a panic(nil) from no panic at all. The old behavior is controlled by the panicnil setting.
package main
import "fmt"
func main() {
defer func() {
r := recover()
fmt.Printf("recovered: %v (%T)\n", r, r)
}()
panic(nil)
}
$ cat go.mod
module example.com/gdb
go 1.27
$ go run .
recovered: runtime error: panic called with nil argument (*runtime.PanicNilError)
$ go mod edit -go=1.20
$ go run .
recovered: <nil> (<nil>)
It's the same toolchain and the same code. With go 1.20 the program gets Go 1.20 behavior, because that's what its author tested against. That's how a newer toolchain can build old code without breaking it, while new code gets the new behavior.
go list shows the GODEBUG defaults that differ from the toolchain's own defaults:
$ go list -f '{{.DefaultGODEBUG}}' . | tr ',' '\n'
containermaxprocs=0
cryptocustomrand=1
...
httpmuxgo121=1
...
panicnil=1
randseednop=0
...
tlssha1=1
...
That's the list for a go 1.20 module. For a go 1.27 module, it's empty.
The go command stores the defaults in the binary. You can see them with go version -m:
$ go build -o gdb.exe . && go version -m gdb.exe
gdb.exe: go1.27.0
path example.com/gdb
mod example.com/gdb (devel)
build -buildmode=exe
build -compiler=gc
build DefaultGODEBUG=panicnil=1
...
Compatibility settings don't last forever. A setting is usually kept for at least two years (four releases) after the change, and some are kept longer. The full list, with the version that introduced each one, is at go.dev/doc/godebug.
Overriding GODEBUG settings
You often want the new go line (to get new language features) but one specific old behavior. There are three ways to set it, from most to least specific.
1. The GODEBUG environment variable at run time overrides everything, which is good for a quick experiment or an emergency fix in production:
$ GODEBUG=panicnil=0 ./gdb.exe
recovered: runtime error: panic called with nil argument (*runtime.PanicNilError)
Several settings are separated by commas: GODEBUG=panicnil=1,httpmuxgo121=1.
2. //go:debug directives in the main package (before the package clause) set build-time defaults for that program:
//go:debug panicnil=1
package main
$ go list -f '{{.DefaultGODEBUG}}' .
panicnil=1
They're valid only in package main and in test files. go vet reports them anywhere else:
$ go vet .
words.go:1:1: //go:debug directive only valid in package main or test
3. A godebug block in go.mod (Go 1.23 and later) applies to every program built in that module, including tests:
module example.com/gdb
go 1.27
godebug (
panicnil=1
httpmuxgo121=1
)
$ go run .
recovered: <nil> (<nil>)
$ go list -f '{{.DefaultGODEBUG}}' .
httpmuxgo121=1,panicnil=1
A single setting can go on one line: godebug panicnil=1. Unknown names are rejected, so typos are caught:
$ tail -1 go.mod
godebug panicnill=1
$ go build .
go: error loading go.mod:
go.mod:5: unknown godebug "panicnill"
You can also set all defaults to those of an older release with godebug default=go1.21, while keeping the newer language version from the go line. Like the toolchain line, the godebug block is only used in the main module.
The settings are also documented for each package: when release notes say something like "this can be reverted with GODEBUG=x509sha256skid=0", that's the name to use in all three places. See Zip archives for an example (zipinsecurepath).
Catching too-new APIs: the stdversion check
The language version check covers syntax, but standard library functions are a different story. Go 1.27 compiles a go 1.23 module that calls strings.Lines (added in 1.24) without complaint, because the function exists in the 1.27 standard library. Someone building your module with Go 1.23, as go.mod allows, would get a compile error.
The stdversion analyzer of go vet (Go 1.23) catches this:
package words
import (
"slices"
"strings"
)
func All(a, b []string) []string {
return slices.Concat(a, b) // added in Go 1.22
}
func CountLines(s string) int {
n := 0
for range strings.Lines(s) { // added in Go 1.24
n++
}
return n
}
$ cat go.mod
module example.com/words
go 1.23
$ go build . && echo build ok
build ok
$ go vet .
words.go:14:20: strings.Lines requires go1.24 or later (module is go1.23)
slices.Concat is fine, because 1.22 is less than 1.23.
In Go 1.27, stdversion is also one of the checks go test runs before the tests, so the problem fails the test run:
$ go test .
# example.com/words
# [example.com/words]
.\words.go:14:20: strings.Lines requires go1.24 or later (module is go1.23)
FAIL example.com/words [build failed]
FAIL
You fix it either by raising the go line (go get go@1.24.0) or by moving the code to a file with a //go:build go1.24 constraint and providing a fallback for older versions in a file with //go:build !go1.24.
Recommendations
- Applications: keep the
goline at the version you actually build and test with, and update it when you update Go. You get the new language features and the new GODEBUG defaults. - Libraries: keep the
goline as low as your users need. Use thetoolchainline if you want to develop with a newer Go, and rely ongo vet(stdversion) to keep you honest. - Use
go get go@Xandgo get toolchain@Xrather than editinggo.modby hand. - When an upgrade breaks something because a behavior changed, add a
godebugentry for that one setting instead of holding back the wholegoline, and plan to remove it. - In CI, decide whether you want
GOTOOLCHAIN=local(test what's installed) or the defaultauto(test whatgo.modasks for), and make it explicit.