go modules
本质是一种GOPATH
之外包管理工具。
Module的状态
- GO111MODULE=off,go命令行将不会支持module功能
- GO111MODULE=on,go命令行会使用modules,而一点也不会去GOPATH目录下查找
- GO111MODULE=auto,默认值
命令
go help 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 tidy //拉取缺少的模块,移除不用的模块。
- go mod download //下载依赖包
- go mod graph //打印模块依赖图
- go mod vendor //将依赖复制到vendor下
- go mod verify //校验依赖
- go mod why //解释为什么需要依赖
- go list -m -json all //依赖详情
go.mod 关键字
- module:模块名称
- require:依赖包列表以及版本
- exclude:禁止依赖包列表
- replace:替换依赖包列表
FAQ
golang dep 导入本地依赖库
目录结构如下:
# cd $GOPATH
# ls src/github.com/x-sso
golang-socials golang-socials-example # 需要引入本地 `github.com/x-sso/golang-socials` 的包
在Gopkg.toml
中添加:
ignored = ["github.com/x-sso/golang-socials/*"]
在go.mod
中添加:
replace github.com/x-sso/golang-socials => ../golang-socials
在golang-socials
目录中执行:
dep ensure -v
go mod vendor