Go如何更新第三方包?

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

How does Go update third-party packages?

问题

看到golang包的增长和改进如此活跃,我想知道如何解决包版本的问题?

我知道一种方法是将第三方包存储在项目文件夹下。

但是如果我使用go get安装它呢?

英文:

Looking how actively golang packages grow and improve I wonder how the problem with package versions is solved?

I see that one way is to store third-party packages under a project folder.

But what if I install it with go get?

答案1

得分: 393

go get将安装在GOPATH中列出的第一个目录中的包(GOPATH是一个环境变量,可能包含一个以冒号分隔的目录列表)。您可以使用go get -u来更新现有的包。

您还可以使用go get -u all来更新GOPATH中的所有包。

对于较大的项目,为每个项目创建不同的GOPATH可能是合理的,这样在项目A中更新库不会导致项目B出现问题。

输入go help gopath以了解有关GOPATH环境变量的更多信息。

英文:

go get will install the package in the first directory listed at GOPATH (an environment variable which might contain a colon separated list of directories). You can use go get -u to update existing packages.

You can also use go get -u all to update all packages in your GOPATH

For larger projects, it might be reasonable to create different GOPATHs for each project, so that updating a library in project A wont cause issues in project B.

Type go help gopath to find out more about the GOPATH environment variable.

答案2

得分: 97

@tux的回答很好,我只想补充一点,你可以使用go get来更新特定的包:

go get -u full_package_name
英文:

@tux answer is great, just wanted to add that you can use go get to update a specific package:

go get -u full_package_name

答案3

得分: 25

由于问题提到了“第三方”库而不是“所有”包,所以您可能希望回退到使用通配符。

一个使用案例是:我只想更新所有从Github VCS获取的包,那么您只需执行以下命令:

go get -u github.com/... //('...'表示通配符)。

这将只更新当前$GOPATH中的github包。

同样适用于VCS内部,假设您只想升级组织A的存储库中的所有包,因为它们发布了您依赖的热修复:

go get -u github.com/orgA/...
英文:

Since the question mentioned third-party libraries and not all packages then you probably want to fall back to using wildcards.

A use case being: I just want to update all my packages that are obtained from the Github VCS, then you would just say:

go get -u github.com/... // ('...' being the wildcard). 

This would go ahead and only update your github packages in the current $GOPATH

Same applies for within a VCS too, say you want to only upgrade all the packages from ogranizaiton A's repo's since as they have released a hotfix you depend on:

go get -u github.com/orgA/...

答案4

得分: 14

上述答案存在以下问题:

  1. 它们会更新所有内容,包括您的应用程序(如果您有未提交的更改)。
  2. 它们会更新您可能已从项目中删除但已存在于磁盘上的软件包。

为了避免这些问题,请按照以下步骤操作:

  1. 删除您想要更新的第三方文件夹。
  2. 进入您的应用程序文件夹并运行 go get -d
英文:

The above answeres have the following problems:

  1. They update everything including your app (in case you have uncommitted changes).
  2. They updated packages you may have already removed from your project but are already on your disk.

To avoid these, do the following:

  1. Delete the 3rd party folders that you want to update.
  2. go to your app folder and run go get -d

答案5

得分: 11

为了指定版本或提交:

go get -u otherpackage@1.2.3

go get -u otherpackage@git-sha

请参阅 https://github.com/golang/go/wiki/Modules#daily-workflow

英文:

To specify versions, or commits:

go get -u otherpackage@1.2.3

go get -u otherpackage@git-sha

See https://github.com/golang/go/wiki/Modules#daily-workflow

答案6

得分: 6

自从这是谷歌搜索的热门结果之一,我只想补充一下,对于1.17版本,“在模块模式下使用'go get'安装可执行文件已被弃用”。

  • go get -d
    • 用于获取当前模块的依赖项
  • go install
    • 用于安装当前模块的要求
  • go install <with_version>
    • 用于忽略当前模块进行安装,例如:'go install example.com/cmd@latest'

https://golang.org/doc/go-get-install-deprecation

英文:

Since this is one of the top hits when googling, I just wanted to add that for 1.17 "installing executables with 'go get' in module mode is deprecated".

  • go get -d
    • For dependencies of the current module
  • go install
    • For requirements of the current module
  • go install &lt;with_version&gt;
    • To install ignoring the current module, i.e: 'go install example.com/cmd@latest'

https://golang.org/doc/go-get-install-deprecation

答案7

得分: 4

go 1.13

(从模块根目录执行)

更新指定的依赖项:

go get -u <package-name>

将所有直接和间接依赖项更新到最新的次要或补丁版本(预发布版本将被忽略):

go get -u ./...
# 或者
go get -u=patch ./...

参考:

https://github.com/golang/go/wiki/Modules#daily-workflow

go help get

英文:

go 1.13

(exec from module root directory)

Update specified dependencies:

go get -u &lt;package-name&gt;

Update all direct and indirect dependencies to latest minor or patch upgrades (pre-releases are ignored):

go get -u ./...
# or
go get -u=patch ./...

Reference:

https://github.com/golang/go/wiki/Modules#daily-workflow

go help get

答案8

得分: 3

如果您想从特定分支升级版本,可以使用:

go get -u <path-to-repo>@<branch>
英文:

If you want to upgrade a version from a specific branch, you can use:

go get -u &lt;path-to-repo&gt;@&lt;branch&gt;

答案9

得分: 2

去到路径并输入

go get -u ./...

它会更新所有需要的包。

英文:

Go to path and type

go get -u ./...

It will update all require packages.

huangapple
  • 本文由 发表于 2012年4月30日 20:11:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/10383498.html
匿名

发表评论

匿名网友

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

确定