Go 在 1.11 提出了 Go Module 作为 Go 的包管理模式。官方文档在 这里,Quick Start 在 这里。
Go 并不强制使用 go module。当在本地磁盘查找一个库时,go 有两种做法:
GOPATH
或者项目的 vendor
目录中寻找go.mod
, go.sum
来安装或者寻找包。包放在 $GOPATH/pkg/mod
中推荐的实践是:
go env -w GO111MODULE=on
go env -w GOPROXY="https://goproxy.cn,direct"
设置 GO111MODULE=on
使得 go 在任何项目都使用 go module 方式;设置 GOPROXY 为 七牛运营的中国镜像,下载速度极快。
Your typical day-to-day workflow can be:
go mod init
.go
code as needed.go build
or go test
will automatically add new dependencies as needed to satisfy imports (updating go.mod
and downloading the new dependencies).go get foo@v1.2.3
, go get foo@master
(foo@default
with mercurial), go get foo@e3702bed2
, or by editing go.mod
directly.A brief tour of other common functionality you might use:
go list -m all
— View final versions that will be used in a build for all direct and indirect dependencies detailsgo list -u -m all
— View available minor and patch upgrades for all direct and indirect dependencies detailsgo get -u ./...
or go get -u=patch ./...
(from module root directory) — Update all direct and indirect dependencies to latest minor or patch upgrades (pre-releases are ignored) detailsgo build ./...
or go test ./...
(from module root directory) — Build or test all packages in the module detailsgo mod tidy
— Prune any no-longer-needed dependencies from go.mod
and add any dependencies needed for other combinations of OS, architecture, and build tags detailsreplace
directive or gohack
— Use a fork, local copy or exact version of a dependency detailsgo mod vendor
— Optional step to create a vendor
directory details如果你到了一个使用了 go modules 的 go 项目代码,你可以运行 go mod tidy
来下载依赖。
go mod why [packages]
可以用来解释一个包是如何被依赖到的,但对于每个包,它只会显示一个最短路径的依赖。
go mod graph
可以把包之间的直接和间接依赖都打出来。可以在其中检索你想看的包。
Go modules 不同于传统的 Go 的依赖管理方式,它单独在 $GOPATH/pkg/mod
下存放它下载的依赖、缓存等:
$ cd $GOPATH/pkg/mod
$ tree -L 3 # Show 3 levels in maximum
9fans.net
│ └── go@v0.0.0-20181112161441-237454027057
│ ├── acme
│ ├── draw
│ ├── games
│ ├── LICENSE
│ ├── plan9
│ ├── plumb
│ └── README
├── cache
│ ├── download
│ │ ├── 9fans.net
│ │ ├── github.com
│ │ ├── golang.org
│ │ ├── gopkg.in
│ │ ├── go.starlark.net
│ │ ├── go.uber.org
│ │ ├── rsc.io
│ │ └── sumdb
│ ├── lock
│ └── vcs
│ ├── 020616345f7c7f88438c217f9d0e26744bce721c80e4a28f93399a8a4cd2acf1
│ ├── 020616345f7c7f88438c217f9d0e26744bce721c80e4a28f93399a8a4cd2acf1.info
│ ├── 020616345f7c7f88438c217f9d0e26744bce721c80e4a28f93399a8a4cd2acf1.lock
│ ├── 0ab7c7bc964e6f4054247fed3ac8d636309918420efe8a7cabef12d9f9904311
│ ├── 0ab7c7bc964e6f4054247fed3ac8d636309918420efe8a7cabef12d9f9904311.info
│ └── 0ab7c7bc964e6f4054247fed3ac8d636309918420efe8a7cabef12d9f9904311.lock
└── github.com
├── labstack
│ ├── echo
│ ├── echo-contrib@v0.9.0
│ ├── echo@v1.4.4
│ ├── echo@v3.3.10+incompatible
│ └── gommon@v0.3.0
└── mattn
├── go-colorable@v0.0.0-20170327083344-ded68f7a9561
├── go-colorable@v0.1.6
├── go-isatty@v0.0.12
├── go-isatty@v0.0.3
├── go-sqlite3@v1.13.0
└── go-sqlite3@v2.0.3+incompatible