go modules包管理

記錄一下go工程遷移go modules的過程。git

go mod

golang從1.11版本以後引入了包管理-go mod,並經過環境變量GO111MODULE 設置:github

  • 默認GO111MODULE 爲auto 在gopath路徑下會從gopath 或者vendor中尋找依賴包,在外部會使用go module的方式尋找依賴包。
  • GO111MODULE =on 只會使用go module的方式尋找依賴包。
  • GO111MODULE =off 只會從gopath中尋找依賴包。golang

    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.sum文件。
    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.sum中進行管理。
    執行go mod vendor會將全部的依賴包複製到工程vendor目錄中。
    go.sum示例:
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

  • If the module is version v2 or higher, the major version of the module must be included as a /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).
  • 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/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.
    來自於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.sum會報錯。
    也能夠是用go mod edit編輯。
  • 執行go mod tidy 增長丟失的module,去掉未用的module。
  • 配置好之後最後再執行go build main.go便會編譯成功。
相關文章
相關標籤/搜索