Go Modules mechanism is the official package management mechanism of Go. It was introduced as an Experiment feature from Go 1.11, and GO111MODULE is the switch of this feature.
GO111MODULE
The GO111MODULE has three values: auto, ON, and off. The default value is auto. The value of the GO111MODULE affects the dependency management mode of the Go Compiler
- Off:
go compiler
Will always useGOPATH mode
Regardless of whether the source directory to build is inGOPATH
Path,go compiler
Will be in the traditionGOPATH
andvendor
Directory, which the target program depends ongo package
- On:
go compiler
Will always usemodule-aware mode
Whether or not the source code to build is locatedGOPATH
Path,go compiler
Will be ingo mod
The cache directory of the command$GOPATH/pkg/mod
Below search for the dependency of the corresponding versionpackage
- Auto:
GOPATH mode
ormodule-aware mode
Depending on whether the source directory to build is located$GOPATH/src
Is the root directory system, and whether to containgo.mod
file
go mod
The command
Golang uses the Go mod command to manage packages
Description of the go mod command:
The command | instructions |
---|---|
download | download modules to local cache |
edit | edit go.mod from tools or scripts |
graph | Print Module Requirement graph |
init | initialize new module in current directory |
tidy | add missing and remove unused module |
vendor | make vendored copy of dependencies |
verify | verify dependencies have expected content |
why | explain why packages or modules are needed |
Use the go mod command:
- Execute the command
go mod init
Will generate one in the current directorygo.mod
File. If the directory already exists before executing this commandgo.mod
File, need to delete first- If the generated
go mod
If the file is not complete, continuego mod tidy
Command, which adds missing modules and removes unwanted modules, is generated when executedgo.sum
file- perform
go mod verify
To check whether the dependencies of the current module are all downloaded, and whether they have been modified after downloading, if there is no problem with the dependencies, it will printall modules verified
- Execute the command
go mod vendor
generatevendor
Directory, which stores thego.mod
The file describes the dependency package, as well as onemodules.txt
go mod
The relevant documents
After executing the go mod init and go mod Tidy commands, two files are generated, respectively:
go.mod
: contains the module name,go
The version and dependencies of the modulego.sum
: Checksum of all dependencies of the module