Golang包管理工具之govendor的使用
本文個人部落格地址為:ofollow,noindex" target="_blank">http://www.huweihuang.com/article/golang/govendor-usage/
本文個人部落格地址為:https://huweihuang.net/article/golang/govendor-usage/
更多golang包管理工具參考golang包管理工具之glide的使用
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]# ls commandsmain.govendormytool_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]# govendor govendor (v1.0.8): record dependencies and copy into vendor folder -govendor-licensesShow govendor's licenses. -versionShow govendor version -cpuprofile 'file'Writes a CPU profile to 'file' for debugging. -memprofile 'file'Writes a heap profile to 'file' for debugging. Sub-Commands initCreate the "vendor" folder and the "vendor.json" file. listList and filter existing dependencies and packages. addAdd packages from $GOPATH. updateUpdate packages from $GOPATH. removeRemove packages from the vendor folder. statusLists any packages missing, out-of-date, or modified locally. fetchAdd new or update vendor folder packages from remote repository. syncPull packages into vendor folder from remote repository with revisions from vendor.json file. migrateMove packages from a legacy tool to the vendor folder with metadata. getLike "go get" but copies dependencies into a "vendor" folder. licenseList discovered licenses for the given status or import paths. shellRun 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.
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" }