go early to consider the issue of dependency management, built-in go get
commands can be obtained directly corresponding dependence, very convenient, but there is a huge flaw, there is no version of the maintenance and management, and version inconsistencies can cause various compatibility issues, So there have been many rely on third-party management tools, dep
and glide
is one of the best, to go 1.11 official has finally launched its own dependency management tools mod
, and built-in versions go, go mod
most before using simple, powerful, and can automatically compatible third-party tools, a large number of excellent open source libraries have been switched to go mod
, a great tendency to dominate the arena
GO111MODULE
One of the biggest changes is golang project finally no longer rely on $GOPATH
the directory, and before the project due to the import
problem of the mechanism, all the projects are located in $GOPATH/src
the directory, although there is not much problem, but always feel weird, go 1.11 finally adjusted this problem, the code from $GOPATH
the move, and for compatibility before research and development mode, still supports put $GOPATH
under, by GO111MODULE
controlling the environment variable
GO111MODULE=off
: Close mod, look forvendor
directories and$GOPATH
reliance in the pathGO111MODULE=on
: ON mod, based solely ongo.mod
downloads and rely FindGO111MODULE=auto
: Defaults in the non-$GOPATH
path and containsgo.mod
only open mod project
The main command
go mod init # 在新的 go 项目中执行,自动分析依赖,创建 go.sum
go mod tidy # 自动分析依赖,并自动添加和删除依赖
go mod vendor # 创建 vendor 目录,将依赖拷贝到当前的 vendor 文件夹下
go mod download # 手动下载依赖
- Go for a new project, new projects only when implementation of what
go mod init
- After each database update, only the first code corresponding to import database, then perform
go mod tidy
can (can also be usedgo mod download
manually download)
Replace the library version
Manually modify go.mod
require field file, re-run go mod tidy
to
require (
github.com/gin-gonic/gin v1.4.0
)
golang version used in v
the beginning of the three version numbers, where the first digit indicates there is a significant update of the books, when a release v2+
-time version of the library module my-module
should be changed module my-module/v2
, otherwise introduced into the library requires increasing +incompatible
suffix
require (
github.com/lestrrat-go/file-rotatelogs v2.2.0+incompatible
)
GFW solve the problem
For some reason, the domestic network can not access the library on golang.org, but fortunately most of the library has a mirror on github, you can use replace
the command to set the mirror, I came across the following are some of the library's
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
)
After 1.12 GO supports a new environment variable GOPROXY, used to set the dependent proxy address, there are two common addresses: community goproxy.io
and they shoot clouds goproxy.cn
, pro-test easy to use
export GO111MODULE=on
export GOPROXY=https://goproxy.io
Cache
Will be in the local cache, the cache path dependence go mod update $GOPATH/pkg/mod
IDE support
goland
Enable configuration mod
【Goland】→【Preference】→【Go Module (vgo)】→ 【Enable Go Modules (vgo)】→ 【OK】
After enabling mod, goland will automatically check for dependencies, and automatically update go.sum, thereby introducing dependencies, under normal circumstances are crippled, and occasionally does not help, perform manual go mod tidy
can
vscode
vscode seems not updated automatically, manually perform go mod tidy
the restart to take effect after the
link
- Official website: https://blog.golang.org/using-go-modules
- Go Modules: https://blog.csdn.net/ytd7777/article/details/86898187
- goproxy.io: https://goproxy.io/
Please indicate the source
article link: https://tech.hatlonely.com/article/56