golang vendor包管理和go mod

go mod

golang1.11版本添加了go mod來管理項目引用的第三方的包,並且可以和vendor互相切換。
保證go version在1.11以上。你的項目是不能直接在$GOPATH/src下的,但是可以在$GOPATH/src下再建立一個文件來作爲項目根目錄,或者在其他非$GOPATH/src的目錄建立項目。我選擇在$GOPATH/src/p1-dhh來作爲項目根目錄。
想要使用先學習一下go mod的用法。

  1. go mod啓用,項目目錄非$GOPATH/src,用 export GO111MODULE=on|off|auto來控制go mod的開啓。
    on:開啓。off:關閉。auto:自動(如果是在$GOPATH/src下,那麼直接用$GOPATH/src
  2. go mod命令行參數:
go mod <command> [arguments] 含義
download download modules to local cache(下載模塊到本地cache目錄下)
edit edit go.mod from tools or scripts(編輯go.mod文件)
graph print module requirement graph(打印出模塊的需求圖)
init initialize new module in current directory(初始化當前目錄的新模塊)
tidy add missing and remove unused modules(添加缺失的、移除廢棄的模塊)
vendor make vendored copy of dependencies(將依賴模塊的首層拷貝到當前目錄下vendor文件夾)
verify verify dependencies have expected content(驗證模塊依賴)
why explain why packages or modules are needed (解釋包或者模塊爲啥被依賴)
  1. 使用go mod:初始化 go mod init;添加依賴 go mod tidy
  2. go mod 替換golang.org/x
module p1-dhh
go 1.12
replace (
	golang.org/x/net => github.com/golang/net latest
	golang.org/x/sync => github.com/golang/sync latest
	golang.org/x/sys => github.com/golang/sys latest
)

golang.org/x的包都有對應的github的鏡像,不知道版本不要緊,統一用latest最新,然後命令行 go mod tidy,就會更新文件了。

vendor

地址:github.com/kardianos/govendor
安裝好以後,試試命令行govendor,如果找不到命令,那就把govendor的可執行文件放到該放的地方,或者配置好環境變量。

  1. govendor命令參數:
govendor <command> 含義
init Create the “vendor” folder and the “vendor.json” file.(創建vendor/vendor.json到當前目錄)
list List and filter existing dependencies and packages.(列出依賴包)
add Add packages from $GOPATH.(從$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.(從遠程存儲庫添加新的或更新vendor文件的包)
sync Pull packages into vendor folder from remote repository with revisions from vendor.json file.(將包從遠程存儲庫拖到vendor文件夾,並修改json文件)
migrate Move packages from a legacy tool to the vendor folder with metadata.(將包從遺留工具移動到包含元數據的vendor文件夾)
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.
  1. govendor 補充參數:
+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 § package is a main package
+outside +external +missing
+all +all packages
  1. 初始化 govendor init
  2. 添加包 govendor add xxxx

之後如果有新的體會會繼續更新。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章