go has considered the issue of dependency management for a long time, and has built-in go get
command , you can directly obtain the corresponding dependencies, which is very convenient, but there is a huge flaw: there is no version maintenance and management, and inconsistent versions may cause various compatibility issues, so many third-party dependency management tools have emerged, dep
and glide
are the best among them. In go 1.11, the official finally launched its own dependency management tool mod
and built it into the go version. go mod
Easy to use, powerful, and automatically compatible with most previous third-party tools. A large number of excellent open source libraries have been switched to go mod
, which has the potential to unify the world
GO111MODULE
One of the biggest changes is that golang projects finally no longer depend on the $GOPATH
directory. In previous projects, due to import
mechanism problems, all projects were located in $GOPATH/src
directory, although there is no big problem, it always feels weird. go 1.11 finally adjusted this problem and moved the code from $GOPATH
In order to be compatible with the previous R&D mode, it is still supported to be placed under $GOPATH
and controlled through the GO111MODULE
environment variable
-
GO111MODULE=off
: Turn off the mod, look for dependencies in thevendor
directory and the$GOPATH
path -
GO111MODULE=on
: Turn on the mod, only based ongo.mod
Download and find dependencies -
GO111MODULE=auto
: Default value, in a non-$GOPATH
path and containinggo. mod
go mod init # 在新的 go 项目中执行,自动分析依赖,创建 go.sum
go mod tidy # 自动分析依赖,并自动添加和删除依赖
go mod vendor # 创建 vendor 目录,将依赖拷贝到当前的 vendor 文件夹下
go mod download # 手动下载依赖
Copy after login
go mod init # 在新的 go 项目中执行,自动分析依赖,创建 go.sum go mod tidy # 自动分析依赖,并自动添加和删除依赖 go mod vendor # 创建 vendor 目录,将依赖拷贝到当前的 vendor 文件夹下 go mod download # 手动下载依赖
- For a new go project, you only need to execute it when creating a new project
- go After mod init
- go mod tidy
(you can also use
go mod downloadManual download)
go.mod file and re-execute
go mod tidy The version of
require ( github.com/gin-gonic/gin v1.4.0 )
v. The first one indicates a major update of the book. When a
v2 is released, version of the library,
module my-module should be changed to
module my-module/v2, otherwise when introducing the library, you need to add the
incompatible suffix
require ( github.com/lestrrat-go/file-rotatelogs v2.2.0+incompatible )
replace Command to set up the mirror. Here are some libraries I came across
replace ( cloud.google.com/go => github.com/googleapis/google-cloud-go v0.0.0-20190603211518-c8433c9aaceb go.etcd.io/bbolt => github.com/etcd-io/bbolt v1.3.4-0.20191001164932-6e135e5d7e3d go.uber.org/atomic => github.com/uber-go/atomic v1.4.1-0.20190731194737-ef0d20d85b01 go.uber.org/multierr => github.com/uber-go/multierr v1.2.0 go.uber.org/zap => github.com/uber-go/zap v1.10.1-0.20190926184545-d8445f34b4ae golang.org/x/crypto => github.com/golang/crypto v0.0.0-20190605123033-f99c8df09eb5 golang.org/x/exp => github.com/golang/exp v0.0.0-20190510132918-efd6b22b2522 golang.org/x/image => github.com/golang/image v0.0.0-20190523035834-f03afa92d3ff golang.org/x/lint => github.com/golang/lint v0.0.0-20190409202823-959b441ac422 golang.org/x/mobile => github.com/golang/mobile v0.0.0-20190607214518-6fa95d984e88 golang.org/x/net => github.com/golang/net v0.0.0-20190606173856-1492cefac77f golang.org/x/oauth2 => github.com/golang/oauth2 v0.0.0-20190604053449-0f29369cfe45 golang.org/x/sync => github.com/golang/sync v0.0.0-20190423024810-112230192c58 golang.org/x/sys => github.com/golang/sys v0.0.0-20190602015325-4c4f7f33c9ed golang.org/x/text => github.com/golang/text v0.3.2 golang.org/x/time => github.com/golang/time v0.0.0-20190308202827-9d24e82272b4 golang.org/x/tools => github.com/golang/tools v0.0.0-20190608022120-eacb66d2a7c3 google.golang.org/api => github.com/googleapis/google-api-go-client v0.6.0 google.golang.org/appengine => github.com/golang/appengine v1.6.1 google.golang.org/genproto => github.com/google/go-genproto v0.0.0-20190605220351-eb0b1bdb6ae6 google.golang.org/grpc => github.com/grpc/grpc-go v1.21.1 )
goproxy.io and Youpaiyun's
goproxy.cn, personally tested and easy to use
export GO111MODULE=on export GOPROXY=https://goproxy.io
$GOPATH/pkg/mod
go mod tidy
go mod tidy It will take effect after restarting
- Official website: https://blog.golang.org/using-go-modules
- Go Modules: https://blog.csdn.net/ytd7777/article/details/86898187
- goproxy.io: https://goproxy.io/
Link to this article: https://tech.hatlonely.com/article/56