[TOC]

介绍

go dep 依赖管理工具是为应用管理代码的,go get是为GOPATH管理代码的

官方代码地址 https://github.com/golang/dep
官方说明为啥要统一依赖管理 https://github.com/golang/dep/blob/master/docs/FAQ.md#does-dep-replace-go-get
官方文档地址 https://golang.github.io/dep/docs/introduction.html

dep 需要在Go 1.7及更高的版本中使用
安装

本文使用 golang 版本是 go1.9.3 需要自己安装 dep

go get -v -u github.com/golang/dep/cmd/dep
基础帮助参数
 $ 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
  prune Pruning is now performed automatically by dep ensure.
  version Show the dep version information
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.

ensure 确保,确保所有本地状态-代码树、依赖列表、锁、远端oss彼此同步

使用

初始化

dep init
# 更建议使用,因为会很漫长
dep init -v
Gopkg.lockGopkg.tomlvendor
Gopkg.toml and Gopkg.lock are out of syncdep ensure -v

其中

Gopkg.tomlGopkg.lockvendornode_module

依赖管理

# 依赖管理帮助
dep help ensure
# 添加一条依赖
dep ensure -add github.com/bitly/go-simplejson
# 这里 @= 参数指定的是 某个 tag
dep ensure -add github.com/bitly/go-simplejson@=0.4.3
# 添加后,先调用一下新加入的库,然后执行 确保 同步
dep ensure
# 同理建议使用
dep ensure -v
# 更新依赖
dep ensure -update -v
#  删除没有用到的 package
dep prune -v

dep ensure -add 添加依赖后,没有在 vendor 目录中,是因为需要在代码中用到这个库才能正确添加
所以在执行 dep ensure -add 后,先尝试使用一下新添加的库,然后运行 dep ensure -v

依赖查看

dep status
$ dep status
PROJECT CONSTRAINT VERSION REVISION LATEST PKGS USED
golang.org/x/net branch master branch master 461777f 3f473d3 4
golang.org/x/sys branch master branch master 93c9922 93c9922 2
golang.org/x/text v0.3.2 v0.3.2 342b2e1 v0.3.2 17
golang.org/x/tools branch master branch master f8d1dee d4e310b 4
gopkg.in/go-playground/validator.v8 v8.18.2 v8.18.2 5f1438d v8.18.2 1
gopkg.in/yaml.v2 v2.2.2 v2.2.2 51d6538 v2.2.2 1
$ dep status | grep gin
github.com/gin-contrib/sse v0.1.0 v0.1.0 54d8467 v0.1.0 1
github.com/gin-gonic/gin ^1.4.0 v1.4.0 b75d67c v1.4.0 4
github.com/swaggo/gin-swagger branch master branch master dbf6ef4 ddb1576 2
  • 如果需要依赖图查看,可以借助 graphviz 工具
# linux 设备
$ sudo apt-get install graphviz
$ dep status -dot | dot -T png | display
# macOS
$ brew install graphviz
$ dep status -dot | dot -T png | open -f -a /Applications/Preview.app
# windows 先安装 chocolatey https://chocolatey.org/
> choco install graphviz.portable
> dep status -dot | dot -T png -o status.png; start status.png

依赖约束规则

添加依赖常用的用法是

# 依赖指定的版本 0.4.3
dep ensure -add github.com/bitly/go-simplejson@=0.4.3
# 依赖大于等于 0.4.3 的版本
dep ensure -add github.com/bitly/go-simplejson@^0.4.3

依赖更新的规则,常见约束见表

GOPKG.TOMLdep ensure -updateversion"^1.0.0""master"version"=1.0.0"git push --force version"foo"aabbccd...

依赖更新

Gopkg.toml
dep ensure -update -v && dep ensure -v
vendor

依赖管理缓存错误

$GOPATH/pkg/dep/sources

dep 管理存在的问题

vendor 目录跨模块的代码拷贝依赖的仓库没有打 tag
Gopkg.tomlversionbranch
[[constraint]]
  branch = "master"
  name = "github.com/xx/xxx"
  #version = ""

最好的方法还是给依赖仓库打 tag

持续集成使用 dep 管理依赖

这里就不介绍一个 dep 管理实例了,因为环境不一样,使用的CI CD链不一样,除了脚本可以通用,其他内容都超过了本文的范围,所以主要讲些原则上的事情

持续集成时,使用 dep 的基本原则

使用 tag 作为版本号vendor使用 Makefile 或者 shell 控制集成过程参数化部署控制

代码提交版本管理

Gopkg.tomlGopkg.lockvendor

这种方式可以做到开箱即用

Gopkg.tomlGopkg.lock
dep ensure -v
vendor

构建过程的控制

不建议直接使用类似 jenkins 这种虚拟命令行构建输出构建的目标二进制和报告直接使用 docker 容器编排测试服务