[英]How to point Go module dependency in go.mod to a latest commit in a repo?
Starting with v1.11 Go added support for modules.从 v1.11 Go 开始添加了对模块的支持。 Commands
命令
go mod init <package name>
go build
would generate go.mod
and go.sum
files that contain all found versions for the package dependencies.将生成
go.mod
和go.sum
文件,其中包含 package 依赖项的所有找到的版本。
If a module does not have any releases, the latest commit of that module is used.如果模块没有任何版本,则使用该模块的最新提交。 If a module does have releases, the latest one is picked as a dependency.
如果模块确实有版本,则最新的版本将被选为依赖项。
However sometimes I would need functionality that is not in a published release yet, but from a commit made after that release.然而,有时我需要的功能尚未在已发布的版本中,而是来自该版本之后的提交。 How do I set
go.mod
to point not to a release of a module, but to a specific commit in the module's repository?如何将
go.mod
设置为不指向模块的发布,而是指向模块存储库中的特定提交?
It looks like I can do it by hand in go.mod with看起来我可以在 go.mod 中手动完成
module /my/module
require (
...
github.com/someone/some_module v0.0.0-20181121201909-af044c0995fe
...
)
where v0.0.0
does not correspond to the last published release tag, 20181121201909
would be a commit timestamp and af044c0995fe
would be the commit hash?其中
v0.0.0
不对应于最后发布的发布标签, 20181121201909
将是提交时间戳,而af044c0995fe
将是提交 hash? Should such information to be found and entered by hand, or there is a better way?这些信息是应该手工查找和输入,还是有更好的方法?
Just 'go get' at the commit hash you want:只需在您想要的提交哈希处“去获取”:
go get github.com/someone/some_module@af044c0995fe
'go get' will correctly update the dependency files (go.mod, go.sum). 'go get' 将正确更新依赖文件(go.mod、go.sum)。
More information: https://github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies更多信息: https : //github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies
In addition the answer from Everton on using go get github.com/someone/some_module@af044c0995fe
to get a specific commit, you can also use branch names such as:除了埃弗顿关于使用
go get github.com/someone/some_module@af044c0995fe
获取特定提交的答案之外,您还可以使用分支名称,例如:
go get github.com/someone/some_module@master
go get github.com/someone/some_module@dev_branch
Those examples get the latest commit on the corresponding branch.这些示例获取相应分支上的最新提交。
It will still be recorded as a pseudo-version in your go.mod
file, such as v0.0.0-20171006230638-a6e239ea1c69
.它仍然会在您的
go.mod
文件中记录为伪版本,例如v0.0.0-20171006230638-a6e239ea1c69
。 (This helps provide a simple total ordering across all versions based on standard semver ordering). (这有助于提供基于标准semver排序的所有版本的简单总排序)。
I have been banging my head for some time that how it works for everyone and I am not able to run it.一段时间以来,我一直在思考它如何适用于每个人,但我无法运行它。 For me, I had to commit to master branch then only I was able to get it.
对我来说,我必须提交到 master 分支然后才能够得到它。
For go get to work with specific branch, commit id or tag, you need to enable a flag for go module by running below command要使用特定的分支、提交 ID 或标签,您需要通过运行以下命令为 go 模块启用标志
go env -w GO111MODULE=on
去 env -w GO111MODULE=on
after this we will be able to do在此之后,我们将能够做到
go get repo@branchname
go get repo@tag
go get repo@commithash
If you want to temporarily substitute a dependency to a local directory (for example if you work on 2 modules sumultaneously) you can add replace
statement at the end of go.mod
file:如果您想临时替换本地目录的依赖项(例如,如果您同时处理 2 个模块),您可以在
go.mod
文件的末尾添加replace
语句:
module example.com/mypkg
go 1.15
require (
gitlab.com/someone/a_package v0.14.2
)
replace gitlab.com/someone/a_package => ../my_forks/a_package
Also if you put the word latest in place of the tag in the go.mod file it will get changed to the latest tag the modules.此外,如果您将单词 latest 放在 go.mod 文件中的标签位置,它将更改为模块的最新标签。
For example:例如:
module /my/module
require (
...
github.com/someone/some_module latest
...
)
will become会变成
module /my/module
require (
...
github.com/someone/some_module v2.0.39
...
)
after running go mod tidy
运行
go mod tidy
go get your-repo@branch-name
read output with go module version to be added to require
or replace
: go get your-repo@branch-name
read output with go module version 添加到require
或replace
:go list -m -json your-repo@branch-name | jq '.|"\\(.Path) \\(.Version)"'
jq
is not installed on your PC - manually combine Path
and Version
values of result from:jq
- 手动组合结果的Path
和Version
值:go list -m -json your-repo@branch-name
If you want to point to the latest commit of a branch, you can always specify the branch name in you go get
command:如果你想指向一个分支的最新提交,你总是可以在
go get
命令中指定分支名称:
go get your-repo@branch-name
It should add this automatically to your go.mod
file.它应该自动将其添加到您的
go.mod
文件中。
Tested with go 1.16
用
go 1.16
测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.