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 compilerWill always useGOPATH modeRegardless of whether the source directory to build is inGOPATHPath,go compilerWill be in the traditionGOPATHandvendorDirectory, which the target program depends ongo package
  • On:go compilerWill always usemodule-aware modeWhether or not the source code to build is locatedGOPATHPath,go compilerWill be ingo modThe cache directory of the command$GOPATH/pkg/modBelow search for the dependency of the corresponding versionpackage
  • Auto:GOPATH modeormodule-aware modeDepending on whether the source directory to build is located$GOPATH/srcIs the root directory system, and whether to containgo.modfile

go modThe 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:

  1. Execute the commandgo mod initWill generate one in the current directorygo.modFile. If the directory already exists before executing this commandgo.modFile, need to delete first
  2. If the generatedgo modIf the file is not complete, continuego mod tidyCommand, which adds missing modules and removes unwanted modules, is generated when executedgo.sumfile
  3. performgo mod verifyTo 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
  4. Execute the commandgo mod vendorgeneratevendorDirectory, which stores thego.modThe file describes the dependency package, as well as onemodules.txt

go modThe relevant documents

After executing the go mod init and go mod Tidy commands, two files are generated, respectively:

  • go.mod: contains the module name,goThe version and dependencies of the module
  • go.sum: Checksum of all dependencies of the module