Table of Contents
GO111MODULE
Home Backend Development Golang Detailed explanation of golang dependency management mod

Detailed explanation of golang dependency management mod

Apr 08, 2021 pm 04:03 PM
golang

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!

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to safely read and write files using Golang? How to safely read and write files using Golang? Jun 06, 2024 pm 05:14 PM

Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

How to configure connection pool for Golang database connection? How to configure connection pool for Golang database connection? Jun 06, 2024 am 11:21 AM

How to configure connection pooling for Go database connections? Use the DB type in the database/sql package to create a database connection; set MaxOpenConns to control the maximum number of concurrent connections; set MaxIdleConns to set the maximum number of idle connections; set ConnMaxLifetime to control the maximum life cycle of the connection.

Golang framework vs. Go framework: Comparison of internal architecture and external features Golang framework vs. Go framework: Comparison of internal architecture and external features Jun 06, 2024 pm 12:37 PM

The difference between the GoLang framework and the Go framework is reflected in the internal architecture and external features. The GoLang framework is based on the Go standard library and extends its functionality, while the Go framework consists of independent libraries to achieve specific purposes. The GoLang framework is more flexible and the Go framework is easier to use. The GoLang framework has a slight advantage in performance, and the Go framework is more scalable. Case: gin-gonic (Go framework) is used to build REST API, while Echo (GoLang framework) is used to build web applications.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to find the first substring matched by a Golang regular expression? How to find the first substring matched by a Golang regular expression? Jun 06, 2024 am 10:51 AM

The FindStringSubmatch function finds the first substring matched by a regular expression: the function returns a slice containing the matching substring, with the first element being the entire matched string and subsequent elements being individual substrings. Code example: regexp.FindStringSubmatch(text,pattern) returns a slice of matching substrings. Practical case: It can be used to match the domain name in the email address, for example: email:="user@example.com", pattern:=@([^\s]+)$ to get the domain name match[1].

Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Apr 02, 2025 am 09:12 AM

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

How to use predefined time zone with Golang? How to use predefined time zone with Golang? Jun 06, 2024 pm 01:02 PM

Using predefined time zones in Go includes the following steps: Import the "time" package. Load a specific time zone through the LoadLocation function. Use the loaded time zone in operations such as creating Time objects, parsing time strings, and performing date and time conversions. Compare dates using different time zones to illustrate the application of the predefined time zone feature.

Golang framework development practical tutorial: FAQs Golang framework development practical tutorial: FAQs Jun 06, 2024 am 11:02 AM

Go framework development FAQ: Framework selection: Depends on application requirements and developer preferences, such as Gin (API), Echo (extensible), Beego (ORM), Iris (performance). Installation and use: Use the gomod command to install, import the framework and use it. Database interaction: Use ORM libraries, such as gorm, to establish database connections and operations. Authentication and authorization: Use session management and authentication middleware such as gin-contrib/sessions. Practical case: Use the Gin framework to build a simple blog API that provides POST, GET and other functions.

See all articles