Distinguish module language and toolchain versions
Distinguish module language and toolchain versions
The go directive declares the minimum Go version required by the module and controls language semantics for its packages. Since Go 1.21 the minimum is enforced. A toolchain directive suggests a toolchain for work in the main module; the installed executable version and module language version need not be identical. GOTOOLCHAIN=local prevents automatic toolchain switching.
Use a fresh directory for each example, create the listed files (labels are not file contents), and run the commands in a POSIX shell. Use Go 1.23+, local toolchain, and Python 3 only for the synthetic proxy examples. For isolation, set GOWORK=off, GOTOOLCHAIN=local, GOPROXY=off and GOSUMDB=off for these local labs only; use a fresh GOMODCACHE per lab. Proxy examples override GOPROXY to a local file URL. Do not copy disabled checksum settings to real projects.
--- go.mod ---module example.test/app
go 1.21
--- main.go ---package main
import "fmt"
func main() { var fs []func() int for _, v := range []int{1, 2} { fs = append(fs, func() int { return v }) } fmt.Println(fs[0](), fs[1]())}
--- Commands (shell, from project root) ---go list -m -f '{{.GoVersion}}'go run .The Go 1.23.2 compiler uses the module’s Go 1.21 loop-variable semantics here. A newer compiler does not automatically change all language behavior of older modules.
Expected output:
1.212 2Change the module language version
--- go.mod ---module example.test/app
go 1.21
--- main.go ---package main
import "fmt"
func main() { var fs []func() int for _, v := range []int{1, 2} { fs = append(fs, func() int { return v }) } fmt.Println(fs[0](), fs[1]())}
--- Commands (shell, from project root) ---go mod edit -go=1.22go mod edit -toolchain=go1.23.2go list -m -f '{{.GoVersion}}'go run .With Go 1.22 language semantics each declared iteration variable is distinct. The toolchain suggestion does not itself change the go directive. In real upgrades check dependency requirements, release notes and tests rather than merely editing version numbers.
Expected output:
1.221 2Try it locally
Set a module go directive to 1.22 and print GoVersion using go list -m.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Modules and workspaces 09: Distinguish module language and toolchain versions
Check the lesson objective before moving on.
Which directive controls the module language version?
What does GOTOOLCHAIN=local prevent?