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/…

相關文章
相關標籤/搜索