你应该在什么时候升级 go.mod 中的 Go 版本?

huangapple go评论92阅读模式
英文:

When should I upgrade the Go version in go.mod?

问题

通常,一个Go项目会涉及两个版本的Go:

  • 主机上安装的Go版本
  • go.mod中定义的最低Go版本

我不太清楚何时需要升级go.mod中的版本,以及何时不需要(如果有的话)。例如,如果我本地安装了Go 1.16.9,将go 1.19设置在go.mod文件中是否有意义?go.mod中的Go版本应该与本地安装的Go版本相匹配吗?它可以更高或更低吗?

有一点让我困惑的是术语“当前模块所需的最低Go版本”,这类似于Android具有的最低兼容版本吗?go.mod中的较低版本是否意味着该模块可以安装在更广泛的机器上,这是一件好事吗?

英文:

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?

答案1

得分: 2

理想情况下,你希望使用你熟悉的最低版本。

如果一个新的标准库函数被锁定在一个新的Go版本中,那么你可能会决定升级Go以使用该函数。

然而,你应该尊重当你升级Go版本时,你会强制消费你模块的用户使用新版本。你可能不在意这一点,但如果你在意的话,那么使用你能够使用的最低版本是有意义的,以便为最广泛的受众提供服务。

英文:

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.

答案2

得分: 0

这取决于情况。

  1. 使用像GoLand这样的集成开发环境,你可以设置一个与本地安装的Go版本不同的Go版本。IDE会下载并安装所选的版本。但是该版本仅在IDE会话中有效。因此,你的go.mod文件可以与你最初安装的Go版本不同。

  2. 否则,如果你的机器上只有一个版本,比如1.16.9,而你想在go.mod中使用v1.19,你需要安装v1.19或在go.mod中降级。

  3. 另一个问题是你是否应该升级项目的Go版本。同样,这取决于你的项目。我曾经遇到过依赖项版本较高的项目,所以我不得不升级当前项目的Go版本。标准Go库在不同版本之间也会有差异。我在不同版本之间遇到的一个常见问题是依赖项解析。因此,你可能还需要注意这些问题。

英文:

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.

huangapple
  • 本文由 发表于 2023年1月4日 21:31:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75006220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定