go語言的依賴管理最主要的是版本控制問題。
govendor是Golang的依賴包管理工具,它的出現能夠避免不一樣用戶在clone同一個項目後從外部獲取不一樣依賴庫版本的問題。
govendor會將項目須要的依賴包添加到項目的vendor目錄下,而且該目錄中vendor.json文件會保存添加的依賴包的路徑等信息。
Golang項目中的go文件在查找依賴的順序是,先在vendor中查找,vendor沒找到而後在GOPATH中查找,都沒找到最後在GOROOT中查找。
govendor只是用來管理項目的依賴包,若是GOPATH中自己沒有項目的依賴包,則須要經過go get先下載到GOPATH中,再經過govendor add +external拷貝到vendor目錄中。git
安裝govendor:go get -u github.com/kardianos/govendor
若是須要使用vendor的項目本來沒有vendor,那麼須要在該項目的目錄下建立vendor
govendor init
以後若是須要添加依賴包,能夠經過如下兩種方式添加
govendor fetch 依賴包路徑
govendor add 依賴包路徑github
.gitignore的配置:shell
/vendor/* !/vendor/vendor.json
根據vendor.json下載依賴:govendor syncjson
govendor用法例子:app
cd path/to/project govendor init govendor fetch project_url_with_out_http cat vendor/vendor.json
➜ project_name git:(develop) govendor --help govendor (v1.0.9): record dependencies and copy into vendor folder -govendor-licenses Show govendor's licenses. -version Show govendor version -cpuprofile 'file' Writes a CPU profile to 'file' for debugging. -memprofile 'file' Writes a heap profile to 'file' for debugging. Sub-Commands init Create the "vendor" folder and the "vendor.json" file. list List and filter existing dependencies and packages. add Add packages from $GOPATH. update Update packages from $GOPATH. remove Remove packages from the vendor folder. status Lists any packages missing, out-of-date, or modified locally. fetch Add new or update vendor folder packages from remote repository. sync Pull packages into vendor folder from remote repository with revisions from vendor.json file. migrate Move packages from a legacy tool to the vendor folder with metadata. get Like "go get" but copies dependencies into a "vendor" folder. license List discovered licenses for the given status or import paths. shell Run a "shell" to make multiple sub-commands more efficient for large projects. go tool commands that are wrapped: "+status" package selection may be used with them fmt, build, install, clean, test, vet, generate, tool Status Types +local (l) packages in your project +external (e) referenced packages in GOPATH but not in current project +vendor (v) packages in the vendor folder +std (s) packages in the standard library +excluded (x) external packages explicitly excluded from vendoring +unused (u) packages in the vendor folder, but unused +missing (m) referenced packages but not found +program (p) package is a main package +outside +external +missing +all +all packages Status can be referenced by their initial letters. Package specifier <path>[::<origin>][{/...|/^}][@[<version-spec>]] Ignoring files with build tags, or excluding packages from being vendored: The "vendor.json" file contains a string field named "ignore". It may contain a space separated list of build tags to ignore when listing and copying files. This list may also contain package prefixes (containing a "/", possibly as last character) to exclude when copying files in the vendor folder. If "foo/" appears in this field, then package "foo" and all its sub-packages ("foo/bar", …) will be excluded (but package "bar/foo" will not). By default the init command adds the "test" tag to the ignore list. If using go1.5, ensure GO15VENDOREXPERIMENT=1 is set.
命令 | 功能 |
---|---|
init | 初始化 vendor 目錄 |
list | 列出全部的依賴包 |
add | 添加包到 vendor 目錄,如 govendor add +external 添加全部外部包 |
add | PKG_PATH 添加指定的依賴包到 vendor 目錄 |
update | 從 $GOPATH 更新依賴包到 vendor 目錄 |
remove | 從 vendor 管理中刪除依賴 |
status | 列出全部缺失、過時和修改過的包 |
fetch | 添加或更新包到本地 vendor 目錄 |
sync | 本地存在 vendor.json 時候拉去依賴包,匹配所記錄的版本 |
get | 相似 go get 目錄,拉取依賴包到 vendor 目錄 |