使用”-u”参数时,为什么要下载两个版本相同的软件包?

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

Why go get downloads two versions of same package when using -u

问题

我使用go get -u命令获取一个依赖于golang.org/x/text@v0.3.7的包。我注意到它首先下载了golang.org/x/text@v0.3.7,然后下载了golang.org/x/text@v0.4.0

然后我运行了go clean -modcachego get golang.org/x/text,它下载了golang.org/x/textv0.4.0版本,然后再次运行go get -u entgo.io/ent。这次go没有下载golang.org/x/text@v0.3.7

所以,为什么当最新版本不存在本地时,go get -u会同时下载旧版本和最新版本?而当最新版本在本地可用时,为什么它不会下载旧版本?

英文:

I was using go get -u to get a package which depends on golang.org/x/text@v0.3.7. I noticed that it first downloads golang.org/x/text@v0.3.7 then downloads golang.org/x/text@v0.4.0.

Then I ran go clean -modcache and go get golang.org/x/text which downloaded v0.4.0 of golang.org/x/text and then again go get -u entgo.io/ent. This time go didn't download golang.org/x/text@v0.3.7

So, Why go get -u downloads both the old version and latest version when the latest version is not present locally, and Why it doesn't download the old version when the latest version is available locally?

答案1

得分: 1

因为这是一个两步骤的过程:

  1. 获取依赖项
  2. 更新依赖项

从编程的角度来看,没有将这两个步骤合并为一个"获取最新依赖项"的单一关注点的好理由。

根据go命令文档

-u标志指示get命令更新命令行上指定的包的依赖模块,以使用较新的次要或补丁版本。

这意味着-u特别处理的是你正在获取的包所依赖的模块,而不是你正在获取的包的模块。

此外,似乎-u对于Go的习惯方式是不可知的,即将v0版本的任何更改都视为主要版本更改,因此不能轻率地建议出于原则而使用-u。甚至golang.org/x/test的README中都提到:

在达到x/text的1.0.0版本之前,次要版本被视为主要版本。因此,从0.1.0到0.2.0被认为是一个主要版本的升级。

英文:

Because it is a two-step process of

  1. Getting dependencies
  2. Updating dependencies

From a programming standpoint there is no good reason to merge these into a single concern of "Get latest dependencies".

From the go command documentation:

> The -u flag instructs get to update modules providing dependencies of packages named on the command line to use newer minor or patch releases when available.

This means that -u specifically deals with modules which the package you are getting depends on, rather than with the module of the package you are getting.

Furthermore, it appears that -u is agnostic to the Go idiom of treating any change in a v0 version as a major version change, so one cannot lightheartedly recommend using -u out of principle. The README of golang.org/x/test even says:

> Until version 1.0.0 of x/text is reached, the minor version is considered a major version. So going from 0.1.0 to 0.2.0 is considered to be a major version bump.

huangapple
  • 本文由 发表于 2022年11月22日 14:54:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/74528537.html
匿名

发表评论

匿名网友

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

确定