Go1.23.4
go mod
Go mod provides access to operations on modules.Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.Usage:go mod <command> [arguments]The commands are:download download modules to local cacheedit edit go.mod from tools or scriptsgraph print module requirement graphinit initialize new module in current directorytidy add missing and remove unused modulesvendor make vendored copy of dependenciesverify verify dependencies have expected contentwhy explain why packages or modules are neededUse "go help mod <command>" for more information about a command.
go mod vendor
将依赖的包下载到当前项目的vendor目录下,方便查看源码。
go mod graph
列出所有包之间的相互依赖。
go mod init <module_name>
初始化项目。
go mod tidy
整理项目依赖。
go help modules
Modules are how Go manages dependencies.A module is a collection of packages that are released, versioned, and
distributed together. Modules may be downloaded directly from version control
repositories or from module proxy servers.For a series of tutorials on modules, see
https://golang.org/doc/tutorial/create-module.For a detailed reference on modules, see https://golang.org/ref/mod.By default, the go command may download modules from https://proxy.golang.org.
It may authenticate modules using the checksum database at
https://sum.golang.org. Both services are operated by the Go team at Google.
The privacy policies for these services are available at
https://proxy.golang.org/privacy and https://sum.golang.org/privacy,
respectively.The go command's download behavior may be configured using GOPROXY, GOSUMDB,
GOPRIVATE, and other environment variables. See 'go help environment'
and https://golang.org/ref/mod#private-module-privacy for more information.
下载指定版本
go get xxxxx@version
下载指定分支
go get xxxxx@master
go.mod样例
module go.etcd.io/etcd/client/v3go 1.22toolchain go1.22.9require (sigs.k8s.io/yaml v1.2.0go.etcd.io/etcd/api/v3 v3.5.17github.com/astaxie/beego v1.12.1
)require (github.com/beorn7/perks v1.0.1 // indirect
)replace (github.com/astaxie/beego => example.com/internal-store/beego.git v1.12.2go.etcd.io/etcd/api/v3 => ../../apigo.etcd.io/etcd => ./FORBIDDEN_DEPENDENCY
)exclude (example.com/thismodule v1.3.0
)
module 后面的名称还可以多定义一个版本号,比如 /v2,/v3,必须是主版本号且大于等于v2,这样别的项目只能依赖于v2或v3下的版本。
go指令
表示此模块正常运行需要的最小版本号。
toolchain指令
指定工具链,依赖中需要比go指令更高的版本。
require指令
如果没有指明 version 的情况下,则默认先下载打了 tag 的 release 版本,比如 v0.4.5 或者 v1.2.3;如果没有 release 版本,则下载最新的 pre release 版本,比如 v0.0.1-pre1。如果还没有则下载最新的 commit,并由Go生成一个伪版本号,形如 v0.0.0-20200921210052-fa0125251cc4
语法:require module-path module-version
replace指令
使用右边的地址替代左边的地址,但是在项目中 import 还是左边的地址,相当于做了一个重定向。右边可以是不同的地址,不同的版本,或目录。
语法:replace module-path [module-version] => replacement-path [replacement-version]
exclude指令
将某个依赖或者版本排除在外。
retract指令
retract v1.1.0
retract [v1.0.0,v1.0.5]
如果你的项目被其他项目引入,但是你的项目中有的版本存在BUG,那么在你的项目中定义retract指令,Go会检查。
上面例子中的FORBIDDEN_DEPENDENCY
就是让Go显示的报错,因为不存在这个目录,etcd经过了大的变迁,导致前期的仓库有问题,使用replace指令来规范用户的依赖。
go.sum
用户模块的安全性校验 Authenticating modules,记载着每个模块的HASH值,根据 GUSUMDB 和 GOPRIVATE 配置决定是否对下载的包进行checksum,如果校验失败会报错。
参考
go.mod file reference
Go Modules Reference
Managing dependencies