Search code examples
gogo-modules

"go mod tidy" will auto upgrade go version in go.mod?


Previously the project was using go 1.20 in go.mod. I've upgrade my go version to 1.21.0 on local linux machine.

When I run go mod tidy for the project, it upgraded go.mod to use go 1.21.0 automatically.

If this is the expected behavior, is there a way to disable this behaviour, aka. don't auto update the go version in go.mod when run go mod tidy? Because others might didn't upgrade locally yet.


Solution

  • The go mod tidy command does not (currently) alter the go directive in go.mod based only on the local toolchain version. It sets it based on dependencies, and does not need to track patch versions.

    If you have a dependency that requires a minimum of go1.21, then your module requires go1.21, forcing the go tool to update the go directive in go.mod accordingly. It would not be of any use to have your module declare a version prior to go1.21 when go1.21 is actually required to build the module.