這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。go vendor 是go 1.5 官方引入管理組件依賴的方式,1.6正式引入
其基本思路是,將引用的外部包的原始碼放在當前工程的vendor目錄下面,go 1.6以後編譯go代碼會優先從vendor目錄先尋找依賴包;
1.解決的問題:
將源碼拷貝到當前工程的vendor目錄下,這樣打包當前的工程代碼到任意機器的$GOPATH/src下都可以通過編譯,避免項目代碼外部依賴過多,遷移後,
需要多次go get 外包依賴包;而且通過go get 重新拉去的外部依賴包的版本可能和工程開發時使用的不一致,導致編譯錯誤問題。
2.未解決的問題:
無法精確的引用外部包進資料列版本設定,不能指定引用某個特定版本的外部包;只是在開發時,將其拷貝過來,但是一旦外部包升級,vendor下的代碼不會跟著升級,
而且vendor下面並沒有元檔案記錄引用包的版本資訊,這個引用外部包升級產生很大的問題,無法評估升級帶來的風險;
3.解決go vendor未解決的問題 使用govendor ,其有如下好處:
https://github.com/kardianos/govendor
1>.可以平滑的將現有非vendor項目轉換為vendor項目
govendor add inport_out_packagename
2>會產生一個中繼資料檔案,記錄項目工程依賴的外部包,以及其版本資訊
vendor/vendor.json
3>提供命令查看整個工程的依賴關係
goverdor --list
goverdor --list -v
project:
https://github.com/kardianos/govendor
Sub-commands
init Create the "vendor" folder and the "vendor.json" file. #建立一個vendor目錄並產生個空的verdor.json檔案
list List and filter existing dependencies and packages. #查看已經存在的依賴包
add Add packages from $GOPATH. #把$GOPATH中的包拷貝到vendor目錄下
update Update packages from $GOPATH. #把$GOPATH中的封裝更新到vendor目錄下
remove Remove packages from the vendor folder. #從vendor目錄下移除外部依賴包
status Lists any packages missing, out-of-date, or modified locally. #查看缺失的或者本地修改的包
fetch Add new or update vendor folder packages from remote repository. #從遠程程式碼程式庫拉取依賴包到vendor目錄
sync Pull packages into vendor folder from remote repository with revisions #一句本地vendor/verdor.json檔案指定的包機器版本資訊從遠端資源庫拉取資源
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. #等於go get 但是同步外部依賴包到vendor目錄,而不是$GOPATH/src
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
Packages can be specified by their "status".
+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 explicitely 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