Go is a new language with an open attitude. There is no official central library maintained like other languages. Most of Go’s libraries are directly on Github. Of course, the package management development of Go is also tortuous. For example, the python project root directory has the requirement. TXT to record the dependency packages, nodejs is the package. json, and the go package management from the early Go DEP (Gopkg) to vendor to the current Go mod. Let me briefly introduce the current go mod, which is also the official default.

What is go Mod

Go.mod is a new official package management tool introduced in Golang1.11. It is used to solve the problem that there is no local record of the specific version of the dependent package, which facilitates the management of the dependent package. And since go 1.13, the Go Module has become the default dependency management tool for the GO language.

Modules is officially defined as:

Dules is a collection of related Go packages that are units of source code exchange and version control. The Go language commands directly support the use of Modules, including logging and resolving dependencies on other Modules, replacing the old GOPATH based method of specifying which source files to use.

Go mod command

The command instructions
go mod download Download dependency packages locally (default GOPATH/ PKG /mod directory)
go mod edit Edit the go.mod file
go mod graph Print the module dependency diagram
go mod init Initialize the current folder and create the go.mod file
go mod tidy Add missing packages and delete useless packages
go mod vendor Copy the dependencies to the vendor directory
go mod verify Check rely on
go mod why Explain why dependencies are needed

How to use go Mod

Prerequisites: Upgrade go to a later version than 1.11 and set GO111MODULE.

GO111MODULE

The GO111MODULE has three values: off, ON, and auto (default).

  • GO111MODULE=off, the go command line will not support module function, the way to find dependent packages will be the old version of the vendor directory or GOPATH mode to find.

  • GO111MODULE=on, will only use go Module to find dependencies.

  • GO111MODULE=auto, the default value. In the gopath path, it will look for dependent packages from Gopath or Vendor, and in the external mode, it will use go Module to look for dependent packages.

GO PROXY

The purpose of go Module is dependency management, so you can abandon the go Get command when using go Module (but it is not forbidden to use, if you want to specify the version of the package or update the package, it is not necessary to use go get at ordinary times). Due to go network problems, Therefore, goproxy.cn is recommended.

Ali cloud mirror GOPROXY=https://mirrors.aliyun.com/goproxy/ / / / / golang mirror GOPROXY = HTTP: / / https://goproxy.io / / China Qiniuyun provides a free legal proxy, Goproxy.cn, for Gopher in China, which is open source. Just a simple command can use this agent: go env - w GOPROXY = https://goproxy.cn, directCopy the code

Initialize the project

The project needs to be initialized the first time it uses the Go Module (without the go.mod file). In the current directory, run go mod init + module name on the command line to initialize the module

Go mod init testCopy the code

After running it, a go.mod file is generated in the current directory. This is a key file that will be used to manage all subsequent packages.

Once the go.mod file is created, its contents are fully controlled by Go Toolchain. Go Toolchain modifies and maintains go.mod files while executing various commands, such as Go Get, Go Build, and Go Mod.

The go.mod file provides four commands: module, require, replace, and exclude

  • The module statement specifies the package name (path)
  • Dependency module specified by the require statement
  • The REPLACE statement replaces dependency modules
  • The exclude statement can ignore dependency modules

Detection of depend on

go mod tidy
Copy the code

Tidy will detect all imported dependencies in the folder directory and write to the go.mod file. After writing, you will notice that the go.mod file has changed, with the package dependencies and version information introduced being written to the go.mod file, but the dependencies are still not downloaded at this point.

Download the dependent

go mod download
Copy the code

Instead of using Go Get, we need to download the dependencies locally. If you do not set GOPROXY as a domestic mirror, this step will be dead.

At this point, all the dependencies will be downloaded to GOPATH, and the go.sum file will be generated in the root directory, which is the detailed dependencies of the dependencies. However, as we said at the beginning, our project is not in GOPATH, so it is useless to download the packages to GOPATH, so we can not find these packages.

Import dependence

go mod vendor
Copy the code

Executing this command will transfer the dependencies that were just downloaded to GOPATH to the vendor(Automatic New) folder in the root directory of the project, at which point we can use them

run

Run the code to find that go Mod automatically finds dependencies and automatically downloads them and writes them to the go. Mod file.

For some known reason, not all packages will download successfully.

Modules can be replaced with the corresponding github library using the replace directive in the go.mod file, such as:

Replace (golang.org/x/crypto v0.0.0-20190313024323-a1F597eDE03a => github.com/golang/crypto V0.0.0-20190313024323 - a1f597ede03a)Copy the code