1024programmer Java GoModuleLearning


Go Module is used by Go for module (module is the smallest source code dependency management unit in Go) dependency management. It marks the version number of all imported modules, similar to maven in java. Go Module was introduced in Go version 1.11, but the default option is turned off and will not be turned on by default until version 1.13. It is expected that 1.13 will be released in August 2019, so before that, Go Module support must be turned on manually
Use Go Module conditions:

$ 1.go version >=1.11
 $ 2.Set environment variable GO111MODULE=on

Go Module command

After turning on Go Module, run the go mod command, the prompt is as follows:

Go mod provides access to operations on modules.

 Note that support for modules is built into all the go commands,
 not just 'go mod'. For example, day-to-day adding, removing, upgrading,
 and downgrading of dependencies should be done using 'go get'.
 See 'go help modules' for an overview of module functionality.


     go mod  [arguments]

 The commands are:

     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 modules
     vendor make vendored copy of dependencies
     verify verify dependencies have expected content
     why explain why packages or modules are needed

The above are the commands provided by go module for module management. You can view the specific official description of go module through the command go help modules.

Let’s talk about some commonly used commands:
I recently learned to use grpc-go. Due to the frequent timeout when accessing golang.org in China, some module dependencies of grpc-go cannot be pulled down. The official solution for this file is to execute the following command:

go mod edit -replace=google.golang.org/grpc=github.com/grpc/grpc-go@latest
 go mod tidy
 go mod vendor
 go build -mod=vendor

go mod edit is used to edit the go.mod file. The above is to replace the grpc project address with the address on github
go mod tidy pulls necessary modules and removes unused modules
go mod vendor copies the dependent modules to the project’s vendor directory
go build -mod=vendor specifies that the modules required for compilation are obtained from the vendor directory.

Other commands:
go mod init is used to initialize the project and use go module for module management.
go mod download download dependent modules

Changes brought about by the introduction of Go Module

After introducing Go Module, the environment variable GOPATH still exists. After turning on the Go Module function, the role of the environment variable GOPATH has also changed.

1: GOPATH/src path change
go help modules documentation:
>When using modules, GOPATH is no longer used for resolving imports. However, it is still used to store downloaded source code (in GOPATH/pkg/mod) and compiled commands ( in GOPATH/bin)

  • The environment variable GOPATH is no longer used to parse the import package path, that is, the package under the original GOPATH/src/ cannot be found through import
  • After the Go Module function is enabled, the downloaded package will be stored in the $GOPATH/pkg/mod path
  • The $GPATH/bin path function remains the same as before

2: Added go.mod file management dependency
Run the command go mod init. The project is initialized to be managed by go module. After running the command, two files, go.mod and go.sum, will be generated. The file go.mod is used to manage module dependencies. go.sum records dependency change information and is automatically generated and does not require maintenance by us.

init initialize new module in current directory

Run the command go help go.mod to view the description of go.mod. After generating the go.mod file, you can simply learn the syntax of the file.

  • module to define the module path;
  • go to set the expected language version;
  • require to require a particular module at a given version or later;
  • exclude to exclude a particular module version from use; and
  • replace to replace a module version with a different module version.

The official provides a simple and comprehensive example:

module my/thing
 go 1.12
 require other/thing v1.0.2
 require new/thing/v2 v2.3.4
 exclude old/thing v1.2.3
 replace bad/thing v1.4.5 => good/thing v1.4.5

3: go get command changes
After the introduction of Go Module, the go get command was re-implemented.

  • Do not enable Go Module function, go get code implementation


  • Enable Go Module function and go get code implementation


The most direct difference is:

1. The old go get package retrieval process is similar: git clone + go install. After turning on the Go Module function, go get only has the git clone or download process
2 Another difference between the old and new implementations is that the locations where packages are stored are different. The former is stored in the $GOPATH/src directory.�The latter is stored in the $GOPATH/pkg/mod directory
3. After the old go get retrieves the main package, it will loop through the submodules under its repo. The new go get no longer supports submodule submodule pulling.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/770711

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top