記錄一下go工程遷移go modules的過程。git
golang從1.11版本以後引入了包管理-go mod,並經過環境變量GO111MODULE 設置:github
GO111MODULE =off 只會從gopath中尋找依賴包。golang
download download modules to local cache edit edit go.mod from tools or scripts 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 verify verify dependencies have expected content why explain why packages or modules are needed
go mod init [module name]
。在工程根目錄下會產生一個go.sum文件。go build main.go
會依次下載依賴包到gopath/pkg/mod中,並在go.sum中進行管理。go mod vendor
會將全部的依賴包複製到工程vendor目錄中。module server go 1.12 require ( github.com/360EntSecGroup-Skylar/excelize/v2 v2.0.2 github.com/YoungPioneers/blog4go v0.5.9 github.com/a8m/kinesis-producer v0.2.0 github.com/antlr/antlr4 v0.0.0-20191115170859-54daca92f7b0 // indirect github.com/apache/thrift v0.13.0 github.com/astaxie/beego v1.12.0 github.com/aws/aws-dax-go v1.0.1 github.com/aws/aws-sdk-go v0.0.0-20180828194226-46ffe7480c9d github.com/fortytw2/leaktest v1.3.0 // indirect )
go mod 會默認拉取最新的relase tag,若是沒有,便拉取最新的commit記錄。並支持版本控制。
indirect代表是間接引用。
注意:
"If an old package and a new package have the same import path, the new package must be backwards compatible with the old package."
「若是舊軟件包和新軟件包具備相同的導入路徑,則新軟件包必須與舊軟件包向後兼容。」apache
/vN
at the end of the module paths used in go.mod files (e.g., module github.com/my/mod/v2, require github.com/my/mod/v2 v2.0.1
) and in the package import path (e.g., import "github.com/my/mod/v2/mypkg
"). This includes the paths used in go get commands (e.g., go get github.com/my/mod/v2@v2.0.1
. Note there is both a /v2 and a @v2.0.1 in that example. One way to think about it is that the module name now includes the /v2, so include /v2 whenever you are using the module name).example.com/my/mod/mypkg
is a different package than example.com/my/mod/v2/mypkg
, and both may be imported in a single build, which among other benefits helps with diamond dependency problems and also allows a v1 module to be implemented in terms of its v2 replacement or vice versa.import("github.com/360EntSecGroup-Skylar/excelize/v2")
(restful 風格)。replace github.com/gohouse/goroom => /path/to/go/src/goroom
go mod init [package name]
go mod edit
編輯。go mod tidy
增長丟失的module,去掉未用的module。go build main.go
便會編譯成功。