Environment and module version directives
Environment and module version directives
From Go 1.21 onward the go directive in go.mod sets a required minimum toolchain version and the language version for that module. A toolchain directive suggests a preferred toolchain when the module is the main module; it does not lower the minimum. GOTOOLCHAIN controls selection, and the auto mode can select or download another toolchain. For an offline lab set GOTOOLCHAIN=local and GOPROXY=off; this fails rather than silently fetching a newer compiler. Inspect environment values without dumping credentials.
package main
import ( "encoding/json" "fmt" "os/exec")
func main() { out, err := exec.Command("go", "env", "-json", "GOTOOLCHAIN", "GOENV", "GOWORK").Output() if err != nil { panic(err) } var m map[string]string if err = json.Unmarshal(out, &m); err != nil { panic(err) } _, a := m["GOTOOLCHAIN"] _, b := m["GOENV"] _, c := m["GOWORK"] fmt.Println(a, b, c)}An empty GOWORK can mean no workspace is active; GOWORK=off disables workspace mode. GOENV=off disables the user configuration file. go env -w persists defaults, so prefer a process-local setting in CI. GOFLAGS supplies default flags; explicit command flags override them where applicable. GOPRIVATE identifies private module patterns; do not disable checksum verification globally to solve private access problems.
Expected output:
true true trueRead go.mod without fetching modules
package main
import ( "encoding/json" "fmt" "os" "os/exec" "path/filepath")
func main() { dir, err := os.MkdirTemp("", "mod-demo-") if err != nil { panic(err) } defer os.RemoveAll(dir) p := filepath.Join(dir, "go.mod") if err = os.WriteFile(p, []byte(`module example.org/demo
go 1.22.0
toolchain go1.23.2`), 0600); err != nil { panic(err) } cmd := exec.Command("go", "mod", "edit", "-json", p) cmd.Dir = dir cmd.Env = append(os.Environ(), "GOTOOLCHAIN=local", "GOWORK=off", "GOPROXY=off") out, err := cmd.Output() if err != nil { panic(err) } var v struct { Go string Toolchain string } if err = json.Unmarshal(out, &v); err != nil { panic(err) } fmt.Println(v.Go, v.Toolchain)}go mod edit -json reads the file without resolving the dependency graph. This example does not prove that dependencies satisfy the minimum. Use go mod tidy and tests in a controlled environment for that. replace changes dependency resolution in the main module or workspace; downstream users do not inherit your local replacement. Record go.sum and module versions for a release; do not treat a local replacement as a published dependency.
Expected output:
1.22.0 go1.23.2Try it locally
Use go/version to compare a minimum Go version with a candidate. Reject invalid version strings before comparing; test an older candidate and a valid one.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Toolchain and compatibility 04: Environment and module version directives
Check the lesson objective before moving on.
What does GOTOOLCHAIN=local do?
What does the go directive mean from Go 1.21?