始终要求在 go.mod 文件中使用最新版本的依赖项。

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

Always require the latest version of a dependency in go.mod

问题

离开是否这是一个好主意的问题不谈,有没有一种方法可以结构化go.mod文件,以便始终使用最新版本的依赖项?

我发现的一种方法是,例如:

require (
    gonum.org/v1/gonum latest
)

这样在使用go get时会下载并解析为gonum的最新版本。然而,这也会更新我的go.mod文件,删除latest标签。

我应该将go.mod文件保留在我的git仓库中,作为包含latest标签的版本,并允许用户在构建时更新他们的版本等吗?

英文:

Leaving aside whether this is a good idea, is there a way to structure go.mod so that the latest version of a dependency is always used?

The one way that I have discovered to do it is to, for example, have

require (
    gonum.org/v1/gonum latest
)

which downloads and resolves to the latest version of gonum when using e.g. go get. However, that also updates my go.mod file, removing the latest tag.

Should I just keep the go.mod file in my git repository as a version containing the latest tag and allow users' versions to be updated when they build etc.?

答案1

得分: 7

go.mod中没有自动执行此操作的方法。实际上,这是有意设计的:go.mod旨在让go命令以确定性的方式选择一组版本进行构建。如果go命令总是选择依赖项的最新版本,那么所选版本的集合将随时间而变化,而无需用户操作。如果您的某个依赖项总是选择其依赖项的最新版本,那么可能会破坏您的构建,并且很难进行覆盖。

使用go get example.com/mod是保持最新的最佳方法。这需要手动完成,但如果您有大量依赖项,可以使用脚本或CI中的操作来自动化该过程。

英文:

There's no way to do this automatically in go.mod. That's by design actually: go.mod is intended to let the go command select a set of versions deterministically with any build. If the go command always picked the latest version of a dependency, the set of selected versions would change over time without any user action. If one of your dependencies always picked the latest version of one its dependencies, that could break your build, and it would be difficult to override.

go get example.com/mod is the best way to stay up to date. That needs to be done manually, but you can automate it with a script or an action in CI if you have a large number of dependencies.

答案2

得分: 5

只需运行go get <module>

go get会下载最新版本的依赖项,这是它的默认行为,并不是因为你在go.mod中指定了latest

有没有一种方法可以结构化go.mod,以便始终使用最新版本的依赖项?

不,你不应该手动编辑go.mod中的require指令内容。此外,require指令的语法已经定义为:

require module-path module-version

其中module-version可以是一个实际的标记版本或伪版本,例如当你需要一个特定的提交时。

从技术上讲,你可以在require指令中写入latest,但是下次运行go命令时,它会将latest替换为实际的最新(伪)版本标记。它不会保持为latest,否则你将无法确定依赖关系。

相关链接:https://stackoverflow.com/questions/53682247

英文:

Just run go get &lt;module&gt;.

go get downloads the latest version of a dependency because that's what it does, not because you specified latest in go.mod.

> is there a way to structure go.mod so that the latest version of a dependency is always used?

No, you are not supposed to manually edit the content of go.mod require directive yourself anyway. Moreover, the syntax of require directives is defined as:

require module-path module-version

where module-version can be an actual tagged version or a pseudo-version, e.g. when you require a specific commit.

Technically you can write latest in a require directive, but the next time you run a go command, it will replace the word latest with the actual latest (pseudo-)version tag. It will not stay latest, otherwise you wouldn't have deterministic dependencies.

Related: https://stackoverflow.com/questions/53682247

huangapple
  • 本文由 发表于 2021年7月6日 23:29:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/68273365.html
匿名

发表评论

匿名网友

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

确定