Detailed explanation of golang dependency management mod

藏色散人
Release: 2021-04-08 19:56:35
forward
2291 people have browsed it

The following is the golang tutorial column to introduce the golang dependency management mod to everyone. I hope it will be helpful to friends in need!

Detailed explanation of golang dependency management mod

golang dependency management mod

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 the vendor directory and the $GOPATH path
  • GO111MODULE=on: Turn on the mod, only based on go.mod Download and find dependencies
  • GO111MODULE=auto: Default value, in a non-$GOPATH path and containing go. mod
##main command

go mod init     # 在新的 go 项目中执行,自动分析依赖,创建 go.sum
go mod tidy     # 自动分析依赖,并自动添加和删除依赖
go mod vendor   # 创建 vendor 目录,将依赖拷贝到当前的 vendor 文件夹下
go mod download # 手动下载依赖
Copy after login

    For a new go project, you only need to execute it when creating a new project
  1. go After mod init
  2. , every time the library is updated, you only need to import the corresponding library in the code first, and then execute
  3. go mod tidy (you can also use go mod download Manual download)
Library version replacement

Manually modify the require field in the

go.mod file and re-execute go mod tidy The version of

require (
    github.com/gin-gonic/gin v1.4.0
)
Copy after login
golang uses a three-digit version number starting with

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
)
Copy after login
Solving the GFW problem

For some reasons, the domestic network cannot access the libraries on golang.org. Fortunately, most libraries have mirrors on github, which can be used

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
)
Copy after login
After GO 1.12, a new environment variable GOPROXY is supported, which is used to set the dependent proxy address. There are two shared addresses: Community

goproxy.io and Youpaiyun's goproxy.cn, personally tested and easy to use

export GO111MODULE=on
export GOPROXY=https://goproxy.io
Copy after login
Cache

go mod will cache locally after updating dependencies. Cache path

$GOPATH/pkg/mod

IDE support

goland

Enable mod configuration

[Goland]→[Preference ] → [Go Module (vgo)] → [Enable Go Modules (vgo)] → [OK]

After enabling the mod, goland will automatically check the dependencies and automatically update go.sum to introduce the dependent library. Under normal circumstances, it works well. Sometimes it doesn't work. You can manually execute

go mod tidy

vscode

vscode does not seem to update automatically. Please execute it manually.

go mod tidy It will take effect after restarting

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/
Reprinted Please indicate the source
Link to this article: https://tech.hatlonely.com/article/56

The above is the detailed content of Detailed explanation of golang dependency management mod. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template