简体   繁体   English

什么时候升级go.mod中的go版本?

[英]When to upgrade go version in go.mod?

Typically a Go project will deal with 2 versions of Go:通常一个 Go 项目会处理 Go 的两个版本:

  • the version of Go installed on the host machine主机上安装的 Go 版本
  • the minimum version of Go defined in go.mod go.mod 中定义的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).我不太了解何时升级go.mod中的版本与何时不升级(如果是这种情况)。 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?例如,如果我本地安装了 Go 1.16.9,那么在go.mod文件中设置go 1.19是否有意义? Should the go version in go.mod be on par with the locally installed version of Go? go.mod中的 go 版本是否应该与本地安装的 Go 版本相当? Can it be higher or lower?能高点还是低点?

Something that confuses me is the term minimum version of Go required by the current module , ie is this similar to how Android has a minimum-compatibility version?令我感到困惑的是minimum version of Go required by the current module ,即这与 Android 具有最低兼容版本的方式相似吗? 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? go.mod中的较低版本是否意味着该模块可以安装在更广泛的机器上,这是一件好事吗?

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 its important to upgrade Go in order to use that function.如果新的标准库 function 被锁定在新的 Go 版本之后,那么您可能会决定升级 Go 以便使用该 function 很重要。

However you should respect that when you upgrade the Go version, you force that new version upon consumers of your module.但是,您应该尊重这一点,当您升级 Go 版本时,您会将新版本强加给模块的消费者。 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.您可能不关心这一点,但如果您关心,那么使用尽可能低的版本以服务于最大的受众是有意义的。

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.使用像 goland 这样的 IDE,你可以设置一个 go 版本,它与你在本地机器上安装的版本不同。 IDE will download and install the selected version also. IDE 也将下载并安装所选版本。 But that version is valid within your IDE session. So your go.mod can have a different version from go version you installed originally但是该版本在您的 IDE session 中有效。因此您的 go.mod 可能与您最初安装的 go 版本不同
  2. Otherwise, if you only have 1 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.否则,如果您的机器上只有 1 个版本,比如 1.16.9,并且您想在 go.mod 中使用 v1.19,则需要安装 v1.19。 or downgrade it in go.mod或者在 go.mod 中降级
  3. Another question can be whether you should upgrade the go version for your project.另一个问题是您是否应该为您的项目升级 go 版本。 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.我有一些依赖于更高版本的项目,所以我不得不升级我当前项目的 go 版本。 There will be differences with standard go libraries also with version changes.标准 go 库也会随着版本的变化而有所不同。 One common challenge I faced across different versions was around dependency resolution.我在不同版本中面临的一个常见挑战是依赖解析。 So you may want to look out for those also.所以你可能也想留意那些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM