go包管理器

包管理器
如果使用過java和python的包管理,一定對包管理很熟悉。通過包管理可以提高開發效率,把時間放在技術的提升,而不是程式碼的管理和同步上。
govendor簡介
ofollow,noindex">github
golang工程的依賴包經常使用go get命令來獲取,例如:go get github.com/kardianos/govendor ,會將依賴包下載到GOPATH的路徑下。
常用的依賴包管理工具有godep,govendor等,在Golang1.5之後,Go提供了 GO15VENDOREXPERIMENT 環境變數(Go 1.6版本預設開啟該環境變數),用於將go build時的應用路徑搜尋調整成為 當前專案目錄/vendor 目錄方式。通過這種形式,我們可以實現類似於 godep 方式的專案依賴管理。
長期以來,golang 對外部依賴都沒有很好的管理方式,只能從 $GOPATH 下查詢依賴。這就造成不同使用者在安裝同一個專案適合可能從外部獲取到不同的依賴庫版本,同時當無法聯網時,無法編譯依賴缺失的專案。
自 1.5 版本開始引入 govendor 工具,該工具將專案依賴的外部包放到專案下的 vendor 目錄下(對比 nodejs 的 node_modules 目錄),並通過 vendor.json 檔案來記錄依賴包的版本,方便使用者使用相對穩定的依賴。
對於 govendor 來說,主要存在三種位置的包:專案自身的包組織為本地(local)包;傳統的存放在 $GOPATH 下的依賴包為外部(external)依賴包;被 govendor 管理的放在 vendor 目錄下的依賴包則為 vendor 包。
安裝與使用
安裝
go get -u -v github.com/kardianos/govendor
把GOPATH加入到 PATH
中,可以直接使用,不用使用絕對路徑了。
#進入到專案目錄 cd /Users/youdi/go/src/golang #初始化vendor目錄 govendor init #檢視vendor目錄 ls rwxr-xr-xyoudistaff96 BSun Nov 18 22:52:53 2018DemoRpc/ rwxr-xr-xyoudistaff128 BFri Nov2 09:32:52 2018VideoServer/ rwxr-xr-xyoudistaff96 BThu Oct 25 20:16:00 2018cgo/ rwxr-xr-xyoudistaff192 BMon Oct 29 22:39:08 2018cloud/ rwxr-xr-xyoudistaff96 BSun Nov 18 23:54:27 2018coroutinue/ rwxr-xr-xyoudistaff160 BMon Nov5 15:25:12 2018crawler/ rwxr-xr-xyoudistaff128 BFri Oct 26 22:52:49 2018errror/ rwxr-xr-xyoudistaff1 KiBMon Nov5 17:24:14 2018example/ rwxr-xr-xyoudistaff128 BThu Nov1 23:08:53 2018goWeb/ rwxr-xr-xyoudistaff128 BThu Nov1 16:45:35 2018gopl/ rwxr-xr-xyoudistaff224 BThu Oct 25 21:09:52 2018gopl-zh/ rwxr-xr-xyoudistaff256 BSat Oct 27 17:03:15 2018goroutine/ rwxr-xr-xyoudistaff128 BSun Nov 18 22:56:50 2018gorpc/ rwxr-xr-xyoudistaff128 BSat Oct 27 17:34:55 2018http/ rwxr-xr-xyoudistaff128 BSun Oct 28 09:59:14 2018maze/ rwxr-xr-xyoudistaff384 BThu Oct 25 21:09:29 2018quickstart/ rwxr-xr-xyoudistaff96 BSun Oct 28 10:54:10 2018regex/ rwxr-xr-xyoudistaff160 BMon Oct 29 16:14:30 2018rpc/ rwxr-xr-xyoudistaff576 BFri Oct 26 18:57:11 2018rpcDemo/ rwxr-xr-xyoudistaff320 BMon Oct 29 14:32:47 2018spider/ rwxr-xr-xyoudistaff128 BMon Nov 12 11:27:11 2018tenx/ rwxr-xr-xyoudistaff256 BSun Nov 18 22:10:32 2018test/ rwxr-xr-xyoudistaff256 BSun Dec2 20:30:39 2018vendor/ rwxr-xr-xyoudistaff256 BFri Oct 26 17:26:54 2018web/ #將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
govendor只是用來管理專案的依賴包,如果GOPATH中本身沒有專案的依賴包,則需要通過go get先下載到GOPATH中,再通過govendor add +external拷貝到vendor目錄中。Go 1.6以上版本預設開啟GO15VENDOREXPERIMENT環境變數。
govendor使用命令
govendor (v1.0.9): 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.
具體來看,這些包可能的型別如下:
狀態 | 縮寫 | 含義 |
---|---|---|
+local | l | 本地包,即專案自身的包組織 |
+external | e | 外部包,即被 $GOPATH 管理,但不在 vendor 目錄下 |
+vendor | v | 已被 govendor 管理,即在 vendor 目錄下 |
+std | s | 標準庫中的包 |
+unused | u | 未使用的包,即包在 vendor 目錄下,但專案並沒有用到 |
+missing | m | 程式碼引用了依賴包,但該包並沒有找到 |
+program | p | 主程式包,意味著可以編譯為執行檔案 |
+outside | 外部包和缺失的包 | |
+all | 所有的包 |
常見的命令如下,格式為 govendor COMMAND。
通過指定包型別,可以過濾僅對指定包進行操作。
命令 | 功能 |
---|---|
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 目錄 |
上面是命令,我們看一下vendor目錄和配置
ll vendor/ rwxr-xr-xyoudistaff832 BThu Nov 29 23:56:45 2018github.com/ rwxr-xr-xyoudistaff96 BThu Nov 29 23:56:45 2018golang.org/ rwxr-xr-xyoudistaff128 BThu Nov 29 23:56:45 2018google.golang.org/ rwxr-xr-xyoudistaff192 BThu Nov 29 23:56:45 2018gopkg.in/ rwxr-xr-xyoudistaff128 BThu Nov 29 23:56:45 2018quickstart/ rw-r--r--youdistaff27 KiBSun Dec2 20:30:39 2018vendor.json
配置檔案:
cat vendor.json { "comment": "", "ignore": "test", "package": [ { "checksumSHA1": "l1dgFJfzGLG/2AEPyt6ADsnVLPQ=", "path": "github.com/PuerkitoBio/goquery", "revision": "dc2ec5c7ca4d9aae063b79b9f581dd3ea6afd2b2", "revisionTime": "2018-06-07T15:06:10Z" }, { "checksumSHA1": "He9ESHVFPcUu6Q8wB6UWHWx+yTo=", "path": "github.com/anaskhan96/soup", "revision": "00be3d730c89ee1efa038324e9bc7355ad23f93b", "revisionTime": "2018-06-22T12:59:56Z" }, { "checksumSHA1": "Q/2QpI7E35SsNYfaxLsWHFry9k4=", "path": "github.com/andybalholm/cascadia", "revision": "901648c87902174f774fac311d7f176f8647bdaa", "revisionTime": "2018-02-20T18:43:36Z" }, ....