golang version management tool GO111MODULE

Before go1.11 version, language packs you want to go to manage, can only rely on third-party libraries to achieve, for example Vendor,GoVendor,GoDep,Dep,Glide, and so on.

1. Turn GO111MODULE

Environment variable  GO111MODULE to enable or disable the module support, it has three possible values: off, , on, autodefault is  auto.

  • GO111MODULE=off No module support, go look for the package from GOPATH and vendor folders.
  • GO111MODULE=on Module supports, go ignores GOPATH and vendor folders, based only  go.mod download the dependencies.
  • GO111MODULE=auto In the  $GOPATH/src outside and the root directory has  go.mod a file open module supports.

When using the module, GOPATH it is meaningless, but it still will download dependencies are stored in  $GOPATH/src/mod the will the  go install results be placed  $GOPATH/bin in.

2. Define module

Module root directory and all its subdirectories package building blocks, the presence in the root directory of  go.mod files , subdirectories will move toward the parent directory, the directory has been found Lord  go.mod file.

Introducing path module refers to the root directory path module, but also in other subdirectories prefix introduction path.

go.mod The first line defines the module path, that line is regarded as a module.

go.mod File used to define the length of the next module and depending on the current version-dependent, and may also exclude replacement dependent dependency.

module example.com/m 

require (
    golang.org/x/text v0.3.0
    gopkg.in/yaml.v2 v2.1.0 
)

replace (
    golang.org/x/text => github.com/golang/text v0.3.0
)

Without having to write the file can be used  go mod init example.com/m to generate  go.mod a first row, the remaining portion of the file have to worry about in the implementation of  go build, go test, go list automatically generates a command required dependent  require statement.

The official recommended regular maintenance of this file, keeping dependencies are clean. For domestic users, to manually maintain this document is inevitable, because you need to  golang.org/x/text replace github.com/golang/text  .

3. Related command

go list command

go list -m You can view the current version and dependence

go mod command

This sub-command is used to process  go.mod the file.

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

go get command

Get a specific version-dependent, for upgrading and downgrading dependencies. Can automatically modify  go.mod files, but also on the version number may also become dependent. In  go.mod use  exclude exclude package, not  go get down.

Unlike before, it is that the new version  go get can be added at the end of  @ the symbol, is used to specify the version.

It requires the warehouse must  v1.2.0 play tag format, as  v1.2 few zeros will not work and must be semantic , and with  v the prefix version number.

Go GET github.com/gorilla/ MUX # match the latest in a Tag 
Go GET github.com/gorilla/ MUX Latest @ # same as above 
Go GET github.com/gorilla/mux@v1. 6.2     # match v1. 6.2 
Go GET github.com/gorilla/mux@e3702bed2 # match v1. 6.2 
Go GET github.com/gorilla/ MUX # @ c856192 match c85619274f5d 
Go GET github.com/gorilla/mux@master match the master branch #

latest Match Newest tag.

v1.2.6 The full version of the wording.

v1, v1.2 Matching the latest version with the prefix, if the version is the latest  1.2.7, they will match  1.2.7.

c856192 Version hash prefix, branch name, no semantic labels in  go.mod in writing conventions will be used  v0.0.0-20180517173623-c85619274f5d, also referred to as dummy version.

go get The version number can be fuzzy matching, but the  go.mod document only reflects the full version, that is  v1.2.0, v0.0.0-20180517173623-c85619274f5dexcept do not need such a long handwritten version, with  go get or above  go mod -require the full version number can be fuzzy matching, it will match to a written  go.mod file.

go build command

go build -getmode=vendor In the case of open module supports, with this we can return to the era of the use of vendor.

 

reference:

1.  do the project versioning with golang 1.11 module

2. Go 1.11 Module Raiders

Guess you like

Origin www.cnblogs.com/embedded-linux/p/11616183.html