記錄一下go工程遷移go modules的程序,
go mod
golang從1.11版本之后引入了包管理-go mod,并通過環境變數GO111MODULE 設定:
- 默認GO111MODULE 為auto 在gopath路徑下會從gopath 或者vendor中尋找依賴包,在外部會使用go module的方式尋找依賴包,
- GO111MODULE =on 只會使用go module的方式尋找依賴包,
- GO111MODULE =off 只會從gopath中尋找依賴包,
go mod 命令
go mod提供了以下的命令:
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新建工程步驟
- 在工程根目錄下(如果GO111MODULE 為auto則工程不可以在gopath中)
go mod init [module name],在工程根目錄下會產生一個go.mod檔案,
go mod 初始化的時候會自動匯入vendor目錄中的包,
goland也支持使用go mod管理包,配置如圖:

go mod 會貫穿go tool工具鏈,go test, go vet, go build等工具都會先檢查依賴,go mod會自動作業, - 執行
go build main.go會依次下載依賴包到gopath/pkg/mod中,并在go.mod中進行管理,
執行go mod vendor會將所有的依賴包復制到工程vendor目錄中,
go.mod示例:
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."
“如果舊軟體包和新軟體包具有相同的匯入路徑,則新軟體包必須與舊軟體包向后兼容,”
- If the module is version v2 or higher, the major version of the module must be included as a
/vNat 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/[email protected]. 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). - If the module is version v0 or v1, do not include the major version in either the module path or the import path.
- In general, packages with different import paths are different packages. For example, math/rand is a different package than crypto/rand. This is also true if different import paths are due to different major versions appearing in the import path. Thus
example.com/my/mod/mypkgis a different package thanexample.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.
來自于golang wiki,
如果匯入了不兼容的高版本的包時,需要在import時表明版本,例import("github.com/360EntSecGroup-Skylar/excelize/v2")(restful 風格),
- 如果需要匯入本地包,可以編輯go.mod檔案,添加
replace github.com/gohouse/goroom => /path/to/go/src/goroom
,并且需要在本地包根目錄下執行go mod init [package name]
否則go mod在本地包目錄中找不到go.mod會報錯,
也可以是用go mod edit編輯, - 執行
go mod tidy增加丟失的module,去掉未用的module, - 配置好以后最后再執行
go build main.go便會編譯成功,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/61793.html
標籤:Go
上一篇:Go-內置time包
下一篇:GO-切片拷貝以及賦值
