About go module

Go 1.11 from the beginning of the introduction module, for version management.

By using the module, the directory location of the project does not have to be placed under GOPATH.

The current version is 1.13, the following will Go1.13 example to introduce the module.

In Go 1.13, the continued use of temporary environment variable GO111MODULE to set whether to use the module.

1. About GO111MODULE

GO111MODULE can be set off, on, or auto (default).

1.1 GO111MODULE=on

If GO111MODULE = on, go command modules will be used to find dependencies, no longer used GOPATH. Commonly referred to as "module-aware mode".
In the module-aware mode, GOPATH is meaningless, but it still will download the dependencies are stored in the $ GOPATH / pkg / mod, the result will be to go install placed $ GOPATH / bin (if not GOBIN settings).

The case where the module is performed go commands (go build, go test .... etc.), automatically added to go.mod in dependency.

1.2 GO111MODULE=off

If GO111MODULE = off, go command no longer use the module dependencies to find, but look for dependencies in the vendor directory and GOPATH. Commonly referred to as "GOPATH mode."

1.3 GO111MODULE = auto or not provided

If GO111MODULE = auto or not set, go command whether to use the module depends on the existence go.mod file.
If there go.mod files in the current directory, or the parent directory, then go command will use the module, you would not use the module.

Special instructions,
in Go 1.13, if GO111MODULE = auto or not set, and as long as there is go.mod file, it will use the module, even even at GOPATH, too.
Go 1.13 Previously, if at GOPATH, even if GO111MODULE = auto or not set, can not use the module.

2.Modules 与 vendoring

When using the modules, go command will automatically download dependencies (storage path GOPATH / pkg / mod), and completely ignore the vendor directory.

For compatibility with older versions of Go, or want all the dependencies in a directory, go language provides a 'go mod vendor'.
Use 'go mod vendor', can be created in the root directory of a main module 'vendor' directory named, which store all go build, dependencies go test used.

Compile-time, using the 'go build -mod = vendor', so will the use of dependencies vendor directory, rather than using the network and the local cache.
Here vendor directory must be the primary module's top vendor directory, vendor directory other locations will be ignored.

3. References

Module support

Modules

How do I use vendoring with modules? Is vendoring going away?

Guess you like

Origin www.cnblogs.com/lanyangsh/p/12078483.html