Description
Many people believe the go
line in the go.mod
file specifies which Go toolchain to use. This proposal would correct this widely held misunderstanding by making it reality. At the same time, the proposal would improve forward compatibility by making sure that old Go toolchains never try to build newer Go programs.
Define the “work module” as the one containing the directory where the go command is run. We sometimes call this the “main module”, but I am using “work module” in this document for clarity.
Updating the go
line in the go.mod
of the work module, or the go.work
file in the current workspace, would change the minimum Go toolchain used to run go commands. A new toolchain
line would provide finer-grained control over Go toolchain selection.
An environment variable GOTOOLCHAIN
would control this new behavior. The default, GOTOOLCHAIN=auto
, would use the information in go.mod
. Setting GOTOOLCHAIN to something else would override the go.mod
. GOTOOLCHAIN=local
would force use of the locally installed toolchain, and other values would choose specific releases. For example, to test the package in the current directory with Go 1.17.2:
GOTOOLCHAIN=go1.17.2 go test
As part of this change, older Go toolchains would refuse to try to build newer Go code. The new system would arrange that normally this would not come up - the new toolchain would be used automatically. But if forced, such as when using GOTOOLCHAIN=local
, the older Go toolchain would no longer assume it can build newer Go code. This in turn would make it safe to revisit and fix for loop scoping (discussion #56010).
See my talk on this topic at GopherCon for more background.
See the design document for details.