dushaoshuai / dushaoshuai.github.io

https://www.shuai.host
0 stars 0 forks source link

Go: 指定模块的特定版本 #63

Open dushaoshuai opened 1 year ago

dushaoshuai commented 1 year ago

go install github.com/mikefarah/yq/v4@v4.26.1

之前一直没搞明白,go install github.com/mikefarah/yq/v4@v4.26.1 中的 v4 代表什么,一度以为是该项目的 v4 分支。现在看来,是自己对 Go 的模块管理理解不到位。

yq 项目中,v4 分支go.mod 文件中,module 指令行内容为:

module github.com/mikefarah/yq/v4

而在 v3 分支go.mod 文件中,module 指令行内容为:

module github.com/mikefarah/yq/v3

可见,该项目升级 major 版本时,模块路径也会改变。这也是 Go 规范中所期望的行为^1go-redis 也是这么做的^2

所以,v4 是模块路径的一部分。

如何指定版本

指定版本有两种形式,在模块路径后加 @,再加 latest 或者具体版本号^3

如在 go install github.com/mikefarah/yq/v4@v4.26.1 中,github.com/mikefarah/yq/v4 是模块路径,v4.26.1 是版本号。如果是 go install github.com/mikefarah/yq/v4@latest,则是安装最新版本。

See also