Go 包管理工具 govendor 使用指南

专栏地址:技术文章专栏mysql

同时,也欢迎关注个人微信公众号 AlwaysBeta,更多精彩内容等你来。git

govendor 是 go 语言依赖管理工具。github

安装及初始化

安装:sql

go get -u -v github.com/kardianos/govendor
复制代码

初始化:json

# Setup your project.
cd "my project in GOPATH"
govendor init

# Add existing GOPATH files to vendor.
govendor add +external
复制代码

下载依赖包

下面介绍三个命令:api

  • govendor fetch:不但能够下载自身的包,还能够下载依赖。
  • govendor get:如官网所述 Like "go get" but copies dependencies into a "vendor" folder,实际上只复制了依赖包进到 vendor 目录而已。
  • govendor add:Add packages from $GOPATH,意思是从本地加载依赖包。

综上,若是是下载依赖包,必定是用 govendor fetchbash

govendor fetch github.com/gin-gonic/gin@v1.2 # 只拷贝 gin/ 目录的内容,而不包含其子目录
govendor fetch github.com/gin-gonic/gin/...@v1.2 # 能够获得 gin/ 目录,及其全部子目录
复制代码

@v1.2 表示使用 v1.2 版本,其实就是 git tag 为 v1.2 的 revision,这个功能很实用。微信

再说一个可能会碰到的问题,有时候咱们使用第三方依赖包,并且还有 bug,修复以后,指望使用本身仓库的时候,能够这样作:markdown

govendor get 'github.com/go-sql-driver/mysql::github.com/yongxinz/go-mysql'
复制代码

原仓库的 github.com/go-sql-driver/mysql 存在一个小问题,此时指望使用本身修复过的 github.com/yongxinz/go-mysql工具

版本管理

不要将整个 vendor/ 目录的内容都提到 git 仓库,只提交 vendor/vendor.json 文件就能够了。

当咱们拉代码以后,须要安装依赖包时,只须要执行下面这条命令就能够了。

govendor sync
复制代码

.gitignore 文件,重点在最后两行:

# Created by https://www.gitignore.io/api/go
### Go ###
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

### Go Patch ###
/vendor/
!/vendor/vendor.json
复制代码

因此,通常的开发流程能够这样来作:若是是新建项目,先安装 govendor 并初始化,而后经过 govendor 来安装依赖包;若是是已有项目,先从版本库拉下来,而后安装 govendor,再执行同步命令便可。

其余命令

govendor status: 查看当前包状态

govendor list +e: 查看当前项目的依赖可是未被添加到 vendor 中的包

govendor add +e: 添加依赖的包。若是 vendor.json 中存在,可是 vendor 目录下不存在(即 govendor status 显示缺失)的包也会被从新添加

govendor remove +u: 删除在 vendor 下可是未依赖的包

在实际过程当中,有部分包是团队的公共包。 这部分包一般有本身的单独项目,而且已经被咱们添加到 $GOPATH 下,可能就不须要添加到当前项目的 vendor 下。

这时候能够结合 listadd 来使用, 先用 list -no-status +e 列出依赖包,而后使用 grep 过滤,再调用 add 命令添加:

govendor list -no-status +e | grep -v 'myteam/common' | xargs govendor add
复制代码


相关文档:
github.com/kardianos/g…
www.orztu.com/post/using-…
linkscue.com/2018/08/09/…

相关文章
相关标签/搜索