最早的时候,Go所依赖的所有的第三方库都放在GOPATH这个目录下面,也被很多开发者所吐槽。随着Go语言的发展,从v1.5开始开始引入vendor模式,如果项目目录下有vendor目录,那么go工具链会优先使用vendor内的包进行编译、测试等。
下面来介绍两个Golang官方依赖工具。

dep

dep 是 golang 项目依赖管理之一,是官方的实验项目,选 dep 作为 golang 的依赖管理工具也是比较靠谱的。

安装

  • MacOS
    • brew install dep
  • Linux
    • curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
  • Windows
    • go get -u github.com/golang/dep/cmd/dep
    • Windows得自己编译,并且确认把 $GOPATH/bin 添加到环境变量 $PATH

验证

在命令行输入dep并回车,会出现以下提示的话,说明已经安装成功了。

Dep is a tool for managing dependencies for Go projects

Usage: "dep [command]"

Commands:

  init     Set up a new Go project, or migrate an existing one
  status   Report the status of the project's dependencies
  ensure   Ensure a dependency is safely vendored in the project
  version  Show the dep version information
  check    Check if imports, Gopkg.toml, and Gopkg.lock are in sync

Examples:
  dep init                               set up a new project
  dep ensure                             install the project's dependencies
  dep ensure -update                     update the locked versions of all dependencies
  dep ensure -add github.com/pkg/errors  add a dependency to the project

Use "dep help [command]" for more information about a command.

功能介绍

  • dep init
    • 初始化创建vendor目录,并下载项目中的所有依赖包,生成Gopkg.lock和Gopkg.toml配置文件。
    • Gopkg.toml是清单文件,Gopkg.lock是校验描述文件。
  • dep status
    • 用来查看项目依赖的详细信息和状态
  • dep ensure
    • 检查所有的依赖库是否都已经安装,如果没有则立即下载。
  • dep ensure -add 指定依赖包
    • 下载添加新的依赖包,并增量更新清单文件和校验描述文件

注:dep不会每次都去下载,而是会优先在本地仓库搜索,本地仓库未找到即在网络上下载,并添加到本地仓库。仓库位于 $GOPATH/pkg/dep/sources

示例

# dep init
Using ^1.6.3 as constraint for direct dep github.com/spf13/viper
Locking in v1.6.3 (59b1917) for direct dep github.com/spf13/viper
Locking in v1.2.0 (2ef7124) for transitive dep github.com/subosito/gotenv
Using ^0.7.1 as constraint for direct dep github.com/mailru/easyjson
Locking in v0.7.1 (8edcc4e) for direct dep github.com/mailru/easyjson
Locking in v1.3.1 (1ffadf5) for transitive dep github.com/spf13/cast
Locking in v1.0.5 (2e9d26c) for transitive dep github.com/spf13/pflag
Locking in v1.2.2 (694aaef) for transitive dep github.com/mitchellh/mapstructure
Locking in v1.55.0 (39bc4dd) for transitive dep gopkg.in/ini.v1
Using ^1.5.0 as constraint for direct dep github.com/go-sql-driver/mysql
Locking in v1.5.0 (17ef3dd) for direct dep github.com/go-sql-driver/mysql
Locking in v1.2.2 (588a75e) for transitive dep github.com/spf13/afero
Locking in v2.2.8 (53403b5) for transitive dep gopkg.in/yaml.v2
Locking in v1.0.0 (8cb6e5b) for transitive dep github.com/hashicorp/hcl
Locking in v1.0.0 (f5c5f50) for transitive dep github.com/jinzhu/inflection
Locking in master (1957bb5) for transitive dep golang.org/x/sys
Using ^1.2.1 as constraint for direct dep github.com/rs/xid
Locking in v1.2.1 (15d2654) for direct dep github.com/rs/xid
Using ^2.0.0 as constraint for direct dep github.com/gomodule/redigo
Locking in v2.0.0 (9c11da7) for direct dep github.com/gomodule/redigo
Locking in v1.1.0 (94f6ae3) for transitive dep github.com/spf13/jwalterweatherman
Locking in v0.3.2 (342b2e1) for transitive dep golang.org/x/text
Locking in v1.8.1 (de8848e) for transitive dep github.com/magiconair/properties
Locking in v1.7.0 (8e8d2a6) for transitive dep github.com/pelletier/go-toml
Using ^1.9.12 as constraint for direct dep github.com/jinzhu/gorm
Locking in v1.9.12 (79a77d7) for direct dep github.com/jinzhu/gorm
Locking in v1.4.9 (45d7d09) for transitive dep github.com/fsnotify/fsnotify
Using ^1.4.2 as constraint for direct dep github.com/gorilla/websocket
Locking in v1.4.2 (b65e629) for direct dep github.com/gorilla/websocket


# dep status
The status of 4 projects are unknown due to errors. Rerun with `-v` flag to see details.
PROJECT                             CONSTRAINT     VERSION        REVISION  LATEST   PKGS USED
github.com/fsnotify/fsnotify        v1.4.9         v1.4.9         45d7d09   v1.4.9   1   
github.com/go-sql-driver/mysql      ^1.5.0         v1.5.0         17ef3dd   v1.5.0   1   
github.com/gomodule/redigo          ^2.0.0         v2.0.0         9c11da7   v2.0.0   2   
github.com/gorilla/websocket        ^1.4.2         v1.4.2         b65e629   v1.4.2   1   
github.com/hashicorp/hcl            v1.0.0         v1.0.0         8cb6e5b   v1.0.0   10  
github.com/jinzhu/gorm              ^1.9.12        v1.9.12        79a77d7   v1.9.12  2   
github.com/jinzhu/inflection        v1.0.0         v1.0.0         f5c5f50   v1.0.0   1   
github.com/magiconair/properties    v1.8.1         v1.8.1         de8848e   v1.8.1   1   
github.com/mailru/easyjson          ^0.7.1         v0.7.1         8edcc4e   v0.7.1   4   
github.com/mitchellh/mapstructure   v1.2.2         v1.2.2         694aaef   v1.2.2   1   
github.com/pelletier/go-toml        v1.7.0         v1.7.0         8e8d2a6   v1.7.0   1   
github.com/rs/xid                   ^1.2.1         v1.2.1         15d2654   v1.2.1   1   
github.com/spf13/afero              v1.2.2         v1.2.2         588a75e   v1.2.2   2   
github.com/spf13/cast               v1.3.1         v1.3.1         1ffadf5   v1.3.1   1   
github.com/spf13/jwalterweatherman  v1.1.0         v1.1.0         94f6ae3   v1.1.0   1   
github.com/spf13/pflag              v1.0.5         v1.0.5         2e9d26c   v1.0.5   1   
github.com/spf13/viper              ^1.6.3         v1.6.3         59b1917   v1.6.3   1   
github.com/subosito/gotenv          v1.2.0         v1.2.0         2ef7124   v1.2.0   1   
golang.org/x/sys                    branch master  branch master  1957bb5   unknown  1   
golang.org/x/text                   v0.3.2         v0.3.2         342b2e1   unknown  6   
gopkg.in/ini.v1                     v1.55.0        v1.55.0        39bc4dd   v1.55.0  1   
gopkg.in/yaml.v2                    v2.2.8         v2.2.8         53403b5   v2.2.8   1   
failed to fetch updates

go mod

go modules 是 golang 1.11 新加的特性。

  • 模块是相关Go包的集合。
  • modules是源代码交换和版本控制的单元。
  • go命令直接支持使用modules,包括记录和解析对其他模块的依赖性。
  • modules替换旧的基于GOPATH的方法来指定在给定构建中使用哪些源文件。

安装

  • 无需额外安装,只要Go版本大于1.11,就默认支持go mod工具。
  • 设置 GO111MODULE
    • export GO111MODULE=on
    • GO111MODULE 有三个值:off, on和auto(默认值)。
    • GO111MODULE=off,go命令行将不会支持module功能,寻找依赖包的方式将会沿用旧版本那种通过vendor目录或者GOPATH模式来查找。
    • GO111MODULE=on,go命令行会使用modules,而一点也不会去GOPATH目录下查找。
    • GO111MODULE=auto,默认值,go命令行将会根据当前目录来决定是否启用module功能。
    • 当modules 功能启用时,依赖包的存放位置变更为$GOPATH/pkg,允许同一个package多个版本并存,且多个项目可以共享缓存的 module。

验证

在命令行输入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 download
    • 下载依赖包
  • go mod edit
    • 编辑go.mod文件
  • go mod graph
    • 打印模块依赖图
  • go mod init
    • 在当前目录初始化,生成go.mod文件
  • go mod tidy
    • 拉取缺少的模块,移除不用的模块
  • go mod vendor
    • 将依赖复制到vendor下
  • go mod verify
    • 验证依赖是否正确
  • go mod why
    • 解释为什么需要依赖

注:初始化后不会立即下载依赖包,当您在项目目录下执行 go get , go build, go run等命令的时候, 会自动将依赖的包下载, 同时将版本信息写入到 go.mod 文件中,还会生成一个新的 go.sum 详细的记录。

go.mod 示例

module demo

go 1.14

require (
	github.com/astaxie/beego v1.12.1
	github.com/go-sql-driver/mysql v1.5.0
	github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect
	github.com/smartystreets/goconvey v1.6.4
)

关于我

name: yison.li
blog: http://yyeer.com
github: https://github.com/yisonli