search
HomeBackend DevelopmentGolangDetailed explanation of golang dependency management mod

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 # 手动下载依赖

    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
)
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
)
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
)
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
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!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
Golang vs. Python: The Pros and ConsGolang vs. Python: The Pros and ConsApr 21, 2025 am 12:17 AM

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

Golang and C  : Concurrency vs. Raw SpeedGolang and C : Concurrency vs. Raw SpeedApr 21, 2025 am 12:16 AM

Golang is better than C in concurrency, while C is better than Golang in raw speed. 1) Golang achieves efficient concurrency through goroutine and channel, which is suitable for handling a large number of concurrent tasks. 2)C Through compiler optimization and standard library, it provides high performance close to hardware, suitable for applications that require extreme optimization.

Why Use Golang? Benefits and Advantages ExplainedWhy Use Golang? Benefits and Advantages ExplainedApr 21, 2025 am 12:15 AM

Reasons for choosing Golang include: 1) high concurrency performance, 2) static type system, 3) garbage collection mechanism, 4) rich standard libraries and ecosystems, which make it an ideal choice for developing efficient and reliable software.

Golang vs. C  : Performance and Speed ComparisonGolang vs. C : Performance and Speed ComparisonApr 21, 2025 am 12:13 AM

Golang is suitable for rapid development and concurrent scenarios, and C is suitable for scenarios where extreme performance and low-level control are required. 1) Golang improves performance through garbage collection and concurrency mechanisms, and is suitable for high-concurrency Web service development. 2) C achieves the ultimate performance through manual memory management and compiler optimization, and is suitable for embedded system development.

Is Golang Faster Than C  ? Exploring the LimitsIs Golang Faster Than C ? Exploring the LimitsApr 20, 2025 am 12:19 AM

Golang performs better in compilation time and concurrent processing, while C has more advantages in running speed and memory management. 1.Golang has fast compilation speed and is suitable for rapid development. 2.C runs fast and is suitable for performance-critical applications. 3. Golang is simple and efficient in concurrent processing, suitable for concurrent programming. 4.C Manual memory management provides higher performance, but increases development complexity.

Golang: From Web Services to System ProgrammingGolang: From Web Services to System ProgrammingApr 20, 2025 am 12:18 AM

Golang's application in web services and system programming is mainly reflected in its simplicity, efficiency and concurrency. 1) In web services, Golang supports the creation of high-performance web applications and APIs through powerful HTTP libraries and concurrent processing capabilities. 2) In system programming, Golang uses features close to hardware and compatibility with C language to be suitable for operating system development and embedded systems.

Golang vs. C  : Benchmarks and Real-World PerformanceGolang vs. C : Benchmarks and Real-World PerformanceApr 20, 2025 am 12:18 AM

Golang and C have their own advantages and disadvantages in performance comparison: 1. Golang is suitable for high concurrency and rapid development, but garbage collection may affect performance; 2.C provides higher performance and hardware control, but has high development complexity. When making a choice, you need to consider project requirements and team skills in a comprehensive way.

Golang vs. Python: A Comparative AnalysisGolang vs. Python: A Comparative AnalysisApr 20, 2025 am 12:17 AM

Golang is suitable for high-performance and concurrent programming scenarios, while Python is suitable for rapid development and data processing. 1.Golang emphasizes simplicity and efficiency, and is suitable for back-end services and microservices. 2. Python is known for its concise syntax and rich libraries, suitable for data science and machine learning.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version