You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I propose adding a -go flag to the go mod init command. This flag allows users to explicitly specify the Go language version used for the go directive in go.mod rather than relying on the toolchain's default heuristic.
Background
When a user runs go mod init [module-path], the Go toolchain automatically inserts a go directive into go.mod. The version selected is typically the version of the toolchain being used, or 1.(N-n).0 after #74748 shipped in Go 1.26.0.
If a developer wants to initialize a module that targets an older version of Go for compatibility reasons (e.g., a library intended to support the last two major Go releases), they must perform a two-step process.
go mod init [module-path]
go mod edit -go=X.Y.Z
This is counter-intuitive for an initialization command, as it forces the user to initialize with a default they don't want and then immediately edit it.
Proposal
Add a -go flag to the go mod init command.
go mod init [-go=version] [module-path]
Example usage.
go mod init -go=1.25.0 example.com/example
This would produce the following go.mod regardless whether the user is running Go 1.25.X, 1.26.X, or a release candidate.
module example.com/example
go 1.25.0
Rationale
The primary motivation is user experience and consistency.
Consistency: The go mod edit command already supports a -go flag. Bringing this to go mod init makes the toolchain interface more consistent.
Automation: Scripts that scaffold new Go projects would benefit from a single command setup.
Alternatives Considered
Environment Variables: Users could set GOTOOLCHAIN, but this affects the entire execution environment and is overkill for simply writing a version string to a text file.
This change is fully backwards compatible. When the -go flag is omitted, the command defaults to its current behavior. The flag follows the standard -go flag syntax used elsewhere in Go.
Proposal Details
I propose adding a
-goflag to thego mod initcommand. This flag allows users to explicitly specify the Go language version used for thegodirective ingo.modrather than relying on the toolchain's default heuristic.Background
When a user runs
go mod init [module-path], the Go toolchain automatically inserts agodirective intogo.mod. The version selected is typically the version of the toolchain being used, or1.(N-n).0after #74748 shipped in Go 1.26.0.If a developer wants to initialize a module that targets an older version of Go for compatibility reasons (e.g., a library intended to support the last two major Go releases), they must perform a two-step process.
This is counter-intuitive for an initialization command, as it forces the user to initialize with a default they don't want and then immediately edit it.
Proposal
Add a
-goflag to thego mod initcommand.Example usage.
This would produce the following
go.modregardless whether the user is running Go 1.25.X, 1.26.X, or a release candidate.Rationale
The primary motivation is user experience and consistency.
Library Authors: Many maintainers support the two most recent major releases of Go. When initializing a new module, they often want to decide on and set the Go version immediately (e.g., cmd/go: decide and document official recommendation for typical 'go' version in go.mod for open source libraries #77830).
Consistency: The
go mod editcommand already supports a-goflag. Bringing this togo mod initmakes the toolchain interface more consistent.Automation: Scripts that scaffold new Go projects would benefit from a single command setup.
Alternatives Considered
Environment Variables: Users could set
GOTOOLCHAIN, but this affects the entire execution environment and is overkill for simply writing a version string to a text file.Do Nothing: While functional, the two-step process is a minor friction point that has been raised in community discussions (e.g., cmd/go: change
go mod initdefault go directive back to 1.N #77653).Compatibility
This change is fully backwards compatible. When the
-goflag is omitted, the command defaults to its current behavior. The flag follows the standard-goflag syntax used elsewhere in Go.