Go Modules使用教程
引入
ofollow,noindex" target="_blank">https://talks.godoc.org/github.com/myitcv/talks/2018-08-15-glug-modules/main.slide#1
Go Modules介紹
Modules是Go 1.11中新增的實驗性功能,基於vgo演變而來,是一個新型的包管理工具。
常見的包管理工具
- govendor
- dep
- glide
- godep
這些包管理工具都是基於GOPATH
或者vendor
目錄,並不能很好的解決不同版本依賴問題。Modules是在GOPATH
之外一套新的包管理方式。
如何啟用Modules
首先要把go升級到1.11。
升級後,可以設定通過一個環境變數GO111MODULE
來啟用modules:
- GO111MODULE=off,go命令列將不會支援module功能,尋找依賴包的方式將會沿用舊版本那種通過vendor目錄或者GOPATH模式來查詢。
- GO111MODULE=on,go命令列會使用modules,而一點也不會去GOPATH目錄下查詢。
- GO111MODULE=auto,預設值,go命令列將會根據當前目錄來決定是否啟用module功能。這種情況下可以分為兩種情形:當前目錄在GOPATH/src之外且該目錄包含go.mod檔案,或者當前檔案在包含go.mod檔案的目錄下面。
當module功能啟用時,GOPATH
在專案構建過程中不再擔當import的角色,但它仍然儲存下載的依賴包,具體位置在$GOPATH/pkg/mod
。
初始化Modules
Go1.11新增了命令go mod
來支援Modules的使用。
> go help mod Go mod provides access to operations on modules. Note that support for modules is built into all the go commands, not just 'go mod'. For example, day-to-day adding, removing, upgrading, and downgrading of dependencies should be done using 'go get'. See 'go help modules' for an overview of module functionality. Usage: go mod <command> [arguments] The commands are: downloaddownload modules to local cache editedit go.mod from tools or scripts graphprint module requirement graph initinitialize new module in current directory tidyadd missing and remove unused modules vendormake vendored copy of dependencies verifyverify dependencies have expected content whyexplain why packages or modules are needed Use "go help mod <command>" for more information about a command.
首先建立一個專案helloworld:
cd && mkdir helloworld && cd helloworld
然後建立檔案main.go
並寫入:
package main import ( log "github.com/sirupsen/logrus" ) func main() { log.WithFields(log.Fields{ "animal": "walrus", }).Info("A walrus appears") }
初始化mod:
go mod init helloworld
系統生成了一個go.mod
的檔案:
module helloworld
然後執行go build,再次檢視go.mod檔案發現多了一些內容:
module helloworld require github.com/sirupsen/logrus v1.1.1
同時多了一個go.sum
的檔案:
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f2633dfe/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.1.1 h1:VzGj7lhU7KEB9e9gMpAV/v5XT2NVSvLJhJLCWbnkgXg= github.com/sirupsen/logrus v1.1.1/go.mod h1:zrgwTnHtNr00buQ1vSptGe8m1f/BbgsPukg8qsT7A+A= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
go.sum不是一個鎖檔案,是一個模組版本內容的校驗值,用來驗證當前快取的模組。go.sum包含了直接依賴和間接依賴的包的資訊,比go.mod要多一些。
go.mod
有四種指令:module,require,exclude,replace。
- module:模組名稱
- require:依賴包列表以及版本
- exclude:禁止依賴包列表(僅在當前模組為主模組時生效)
- replace:替換依賴包列表 (僅在當前模組為主模組時生效)
其他命令
go mod tidy //拉取缺少的模組,移除不用的模組。
go mod download //下載依賴包
go mod graph //列印模組依賴圖
go mod vendor //將依賴複製到vendor下
go mod verify //校驗依賴
go mod why //解釋為什麼需要依賴
go list -m -json all //依賴詳情