6

Typically a Go project will deal with two versions of Go:

  • the version of Go installed on the host machine
  • the minimum version of Go defined in go.mod

I don't understand well when to upgrade the version in go.mod vs. when not to (if that is ever the case). For example, if I have a locally installed Go 1.16.9, does it make sense to set go 1.19 in the go.mod file? Should the Go version in go.mod be on par with the locally installed version of Go? Can it be higher or lower?

Something that confuses me is the term minimum version of Go required by the current module, i.e., is this similar to how Android has a minimum-compatibility version? Does a lower version in go.mod mean the module can be installed on a wider range of machines and is this a good thing?

2
  • 2
    "if I have a locally installed Go 1.16.9" Update to 1.19. 1.16 is no longer maintained. "oes a lower version in go.mod mean the module can be installed on a wider range of machines and is this a good thing?" No, not really: 1. Its about compilation, not installation and 2. everybody uses the latest (maybe n-1) version of Go anyway.
    – Volker
    Jan 4 at 13:37
  • 2
    All uses of the go directive are spelled out in detail here: go.dev/ref/mod#go-mod-file-go. You can even compile with a newer version than what is listed, but it may fail if it encounters unsupported features. Just make sure it corresponds to the semantics of the version you are using.
    – JimB
    Jan 4 at 13:52

2 Answers 2

2

Ideally you want to use the lowest version you are comfortable with.

If a new standard library function is locked behind a new Go version, then you may decide it's important to upgrade Go in order to use that function.

However you should respect that when you upgrade the Go version, you force that new version upon consumers of your module. You may not care about that, but if you do, then it makes sense to use the lowest version you can, in order to serve the largest audience.

0

It depends.

  1. With IDEs like GoLand, you can set up a Go version which is different from what you have installed on your local machine. The IDE will download and install the selected version also. But that version is valid within your IDE session. So your go.mod file can have a different version from the Go version you installed originally

  2. Otherwise, if you only have one version on your machine, say 1.16.9 and you want to use v1.19 in your go.mod, you would need to install v1.19. or downgrade it in go.mod

  3. Another question can be whether you should upgrade the Go version for your project. Again, it depends on your project. I have had projects where dependencies were on higher version, so I had to upgrade my current project's Go version. There will be differences with standard Go libraries also with version changes. One common challenge I faced across different versions was around dependency resolution.
    So you may want to look out for those also.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.