本文個人部落格地址為:http://www.huweihuang.com/article/golang/govendor-usage/
本文個人部落格地址為:https://huweihuang.net/article/golang/govendor-usage/
更多golang包管理工具參考
- dep的使用
- glide的使用
- govendor的使用
1. govendor簡介
golang工程的依賴包經常使用go get命令來擷取,例如:go get github.com/kardianos/govendor ,會將依賴包下載到GOPATH
的路徑下。
常用的依賴包管理工具有godep
,govendor
等,在Golang1.5之後,Go提供了 GO15VENDOREXPERIMENT
環境變數(Go 1.6版本預設開啟該環境變數),用於將go build時的應用路徑搜尋調整成為 當前項目目錄/vendor
目錄方式。通過這種形式,我們可以實作類別似於 godep
方式的項目依賴管理。
2. 安裝與使用
2.1. 安裝
go get -u -v github.com/kardianos/govendor
2.2. 使用
#進入到項目目錄cd /home/gopath/src/mytool#初始化vendor目錄govendor init#查看vendor目錄[root@CC54425A mytool]# lscommands main.go vendor mytool_test.sh#將GOPATH中本工程使用到的依賴包自動移動到vendor目錄中#說明:如果本地GOPATH沒有依賴包,先go get相應的依賴包govendor add +external或使用縮寫: govendor add +e #Go 1.6以上版本預設開啟 GO15VENDOREXPERIMENT 環境變數,可忽略該步驟。#通過設定環境變數 GO15VENDOREXPERIMENT=1 使用vendor檔案夾構建檔案。#可以選擇 export GO15VENDOREXPERIMENT=1 或 GO15VENDOREXPERIMENT=1 go build 執行編譯export GO15VENDOREXPERIMENT=1
2.3. 說明
govendor
只是用來管理項目的依賴包,如果GOPATH
中本身沒有項目的依賴包,則需要通過go get
先下載到GOPATH中,再通過govendor add +external
拷貝到vendor
目錄中。Go 1.6以上版本預設開啟GO15VENDOREXPERIMENT環境變數。
3. govendor使用命令
[root@CC54425A mytool]# govendorgovendor (v1.0.8): 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, toolStatus 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.
4. vendor.json
govendor設定檔,記錄依賴包列表。
{ "comment": "", "ignore": "test", "package": [ { "checksumSHA1": "uGalSICR4r7354vvKuNnh7Y/R/4=", "path": "github.com/urfave/cli", "revision": "b99aa811b4c1dd84cc6bccb8499c82c72098085a", "revisionTime": "2017-08-04T09:34:15Z" } ], "rootPath": "mytool"}