![有,总比没有要好:Go依赖管理工具dep](http://p2.ttnews.xyz/loading.gif)
介绍
早期的Golang被很多开发者所诟病的一个问题就是依赖包的管理。Golang 1.5 release版本的发布之前,只能通过设置多个GOPATH的方式来解决这个问题,例如:我两个工程都依赖了Beego,但A工程依赖的是Beego1.1,B工程依赖的是Beego1.7,我必须设置两个GOPATH来区分,并且在切换工程的时候GOPATH也得切换,无比痛苦。终于终于在Golang 1.5 release 开始支持除了GOROOT和GOPATH之外的依赖查询,那就是vender,于是很多大佬开始造轮子,造得比较好的应该是dep和glide。
主角出场
今天的主角是dep,Golang的官方依赖工具,用来管理和下载工程依赖的工具,以下是官方的介绍
dep is a prototype dependency management tool for Go. It requires Go 1.9 or newer to compile. dep is safe for production use.安装
- 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并回车,会出现以下提示的话,说明已经安装成功了。
![有,总比没有要好:Go依赖管理工具dep](http://p2.ttnews.xyz/loading.gif)
功能介绍
- dep init
备份当前的vender,创建vender目录并下载项目中的所有依赖包,生成Gopkg.lock和Gopkg.toml
以下是两个文件的作用说明,简单讲Gopkg.toml是清单文件,Gopkg.lock是校验描述文件。尽量不要修改,避免造成两个文件不同步的错误。
A manifest - a file describing the current project's dependency requirements. In dep, this is the Gopkg.toml file. A lock - a file containing a transitively-complete, reproducible description of the dependency graph. In dep, this is the Gopkg.lock file.- dep status
- 用来查看项目依赖的详细信息和状态,非常清晰。
- dep ensure
- 尝试确保所有的依赖库都已经安装,如果没有即下载,相当于对依赖库做增量更新。
- dep ensure add github.com/RoaringBitmap/roaring@^1.0.1
- 下载添加新的依赖库,并增量更新清单文件和校验描述文件。github.com/RoaringBitmap/roaring 是依赖库的包名,1.0.1是库的版本号。
本地缓存
当然dep不会每次都去下载,其工作原理和Mevan一样,会优先在本地仓库搜索,本地仓库未找到即在网络上下载,并添加到本地仓库。
$GOPATH/pkg/dep/sources
閱讀更多 隨一暢想 的文章