Golang: Package Management

15th January 2021 at 8:39pm
Golang

Go 在 1.11 提出了 Go Module 作为 Go 的包管理模式。官方文档在 这里,Quick Start 在 这里

Go 并不强制使用 go module。当在本地磁盘查找一个库时,go 有两种做法:

  • 传统方式:从 GOPATH 或者项目的 vendor 目录中寻找
  • go module 方式:根据 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 为 七牛运营的中国镜像,下载速度极快。

Daily Workflow

Your typical day-to-day workflow can be:

  • go mod init
  • Add import statements to your .go code as needed.
  • Standard commands like go build or go test will automatically add new dependencies as needed to satisfy imports (updating go.mod and downloading the new dependencies).
  • When needed, more specific versions of dependencies can be chosen with commands such as 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 details
  • go list -u -m all — View available minor and patch upgrades for all direct and indirect dependencies details
  • go 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) details
  • go build ./... or go test ./... (from module root directory) — Build or test all packages in the module details
  • go 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 details
  • replace directive or gohack — Use a fork, local copy or exact version of a dependency details
  • go mod vendor — Optional step to create a vendor directory details

如果你到了一个使用了 go modules 的 go 项目代码,你可以运行 go mod download 来下载依赖。

Go modules 如何维护一个包的多个版本?

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