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 cache
edit edit go.mod from tools or scripts
graph print module requirement graph
init initialize new module in current directory
tidy add missing and remove unused modules
vendor make vendored copy of dependencies
verify verify dependencies have expected content
why explain why packages or modules are needed
Use "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/v3
go 1.22
toolchain go1.22.9
require (
sigs.k8s.io/yaml v1.2.0
go.etcd.io/etcd/api/v3 v3.5.17
github.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.2
go.etcd.io/etcd/api/v3 => ../../api
go.etcd.io/etcd => ./FORBIDDEN_DEPENDENCY
)
exclude (
example.com/thismodule v1.3.0
)
module 后面的名称还可以多定义一个版本号,比如 /v2,/v3,必须是主版本号且大于等于v2,这样别的项目只能依赖于v2或v3下的版本。
还要一种使用 v2 的方式,比如在Go1.22及以上版本中的math/rand/v2
,它是rand的v2版本,相当于重新了一套,此时你只需要引入math/rand/v2
就是使用新版本。
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文件。那么Go会在current directory or any parent directory
寻找go.mod文件,这个current directory
或者称为working directory
,它指的是cmd命令行所在的目录,而不是你的目标go文件的目录。比如我有两个项目a和b。
D:\dev\php\magook\trunk\server\a
D:\dev\php\magook\trunk\server\b
那么如下命令,会在项目a的go.mod环境下运行项目b中的main.go
cd D:\dev\php\magook\trunk\server\a
go run D:\dev\php\magook\trunk\server\b\main.go
这就是工作目录的意义,在exec.Cmd.Dir
就是设置working directory
。