Go Modules Detailed

Original link: Go Modules Detailed

Go 1.11 and Go 1.12 contain a preliminary Go Modules support, and plans to release in August 2019 using the Go 1.13 Go Modules by default in all development processes.

Go Modules is to enhance the use of other developers code is added ** dependency (modules, packages) ** experience when, but also to let the correctness of the code, security guaranteed. And Go Modules can be used GOPROXY environment variable to solve the problem in mainland China can not go get use of.

So now learn about Go Modules are necessary.

Golang Logo

mode

Go Modules There are three modes in Go 1.11 and Go 1.12, GO111MODULE environment variable is defined according to:

  • Default mode (or not set the environment variable GO111MODULE=auto): Go Go Modules use the command-line tool when the following conditions are true:
    • The current directory is not GOPATH / src / down;
    • There is go.mod file in the current directory or the upper directory.
  • GOPATH mode ( GO111MODULE=off): Go command-line tool never use Go Modules. Instead, it looks GOPATH and vendor directory to find dependencies.
  • Go Modules mode ( GO111MODULE=on): Go command-line tool uses only Go Modules, never consulting GOPATH. GOPATH no longer as the import directory, but it is still stored download dependencies (GOPATH / pkg / mod /) and a command installed (GOPATH / bin /), only removed GOPATH / src /.

Go 1.13 Go Modules default mode, so the above can be ignored after Go 1.13 release and use in a production environment.

Core file: go.mod

The following is an example of one of the most comprehensive go.mod file:

module my/thing
go 1.12
require other/thing v1.0.2 // 这是注释
require new/thing/v2 v2.3.4 // indirect
require(
  new/thing v2.3.4
  old/thing v0.0.0-20190603091049-60506f45cf65
)
exclude old/thing v1.2.3
replace bad/thing v1.4.5 => good/thing v1.4.5
复制代码

Very comprehensive and very complex. But in fact go.mod file is not so complicated in actual projects, and once the file exists, no additional steps: Like go build, go test, and even go list this command will automatically add new dependencies needed to meet import.

But now we come to learn more about the composition of go.mod file:

go.mod files are line-oriented current module (main module) is generally located in the first row, the path followed by ordering dependencies.

Each row contains a command, by a verb followed by a preamble parameters.

All action verbs preamble as follows:

  • module: Definition module path.
  • go: Set the desired language version.
  • require: Request to a given version of a particular module or later.
  • exclude: Preclude the use of a particular version of the module, the module version does not allow to be considered unavailable and the query can not be returned.
  • replace: Replace the original module using a different version of the module version.

Preamble verbs can also press the block mode is used to create a block (lines 5-8) in brackets, as in the introduction of Go:

import (
    "errors"
    "fmt"
    "log"
)
复制代码

Note (lines 3-4) may use a single line // 这是注释comment, but can not use multi-line /* 这是注释 */comments. And indirectNote (line 4) marked, but is indirectly introduced into the module is not directly introduced into the current module.

go.mod file exists only in the root directory module, subdirectory introduction path uses path module subdirectory path introduction + form. For example: If the name of a subdirectory is created world, you do not need to be used in subdirectory go mod initcommand, Go command line tool is automatically recognized as a part of a module hello, so its introduction path hello / world.

Go command-line tool automates the process module version go.mod specified. When the source code importwhen you point the module does not exist in go.mod file, Go command-line tool will automatically search for the module, and the latest version (the last one tag and non-stable pre-release version) added to go.mod file .

If there is no tag, using a pseudo-version (line 7), which is a version of the syntax, specifically for the flag is not submitted (package under some golang.org/x/ is without a tag) tag is. Such v0.0.0-20190603091049-60506f45cf65as: .

The front part of the semantic version number, the version for marking; UTC time for the submission of the intermediate portion for comparing two versions of its pseudo-determined sequence; commit rear portion is a prefix of the hash, which is located in this version for marking commit.

Version management file: go.sum

Examples are as follows:

github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-gonic/gin v1.4.0 h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ=
github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
复制代码

Each row path introduced by the module, the module specific version expected hash composition.

Each time the module is missing, if the cache does not exist, it is necessary to download and added to calculate the hash go.sum; if present in the cache, it is necessary to match the existing entry go.sum.

In this way, users can use the software to build a hash to verify whether its construction with you to build the same ( go mod verify), regardless of how they acquired dependency, you can get the same version. But also ensure the project is not expected to occur dependent maliciously modified outside and other issues. This is also the reason why should go.sum file version management (Git) was added.

Coupled with Go Modules chose the smallest version of the selected strategy (using the default build each module involved in the oldest version allowed that the new release has no effect on the building) can be achieved to build reproducible (built on repeat when the same result).

Semantic version

What is the semantic version ? Is a semantic version agreed upon by the co-founder Tom Preston-Werner Gravatars established by the founder and GitHub. Under this agreement, the semantics of the version number of the update mode and contains a lot of useful information.

Semantic version format: X.Y.Z(. Time major version version revision number), using the following method:

  • When changes are not backward compatible, incrementing the major version number.
  • When the API backward compatibility of new and modification, the minor version number is incremented.
  • When fix the problem but does not affect the API, incremental revision number.

For example, a semantic version number: v0.1.2, its major version number is 0, the second version 1, revision number is 2. The front vis a version (version) of the first letter, is the Go language conventions used, standard semantic version does not have this convention.

Therefore, when using the Go command line tools or go.mod file, you can use the version number to the semantic module queries the following rules:

  • The default value ( @latest): The label does not match the latest version of the latest available version or label source library.
  • Fully specified version ( @v1.2.3): will match the specified version.
  • Version prefix ( @v1or @v1.2): The match with the latest available version of the label prefix.
  • Version Comparison ( @<v1.2.3or @>=v1.5.6): the closest match label version is available relatively goals. <It was less than the latest version of the release, >was greater than the version of the oldest version. When using a Unix system, enclose the string wrapped required to prevent less than a number greater than to be construed as redirected. Such go get 'github.com/gin-gonic/gin@<v1.2.3'as: .
  • Specify a the commit ( @c856192): the matching version of the commit.
  • Designate a branch ( @master): will match the branch version.

Semantic import version

As shown above, in order to allow users Go Modules can more easily upgrade the old version to the new version from, Go official languages ​​raises two important rules:

  • Introducing compatibility rules (import compatibility rule): If the old and the new packet having the same packet introducing path, the new packet must be backwards compatible with the old package.
  • Import version semantic rules (semantic import versioning rule): each different major version (i.e., incompatible packets v1or v2) the use of different inlet path, ending in the main version, and each version up to a master. Such as: a rsc.io/quote, a rsc.io/quote/v2, a rsc.io/quote/v3.

Git branch integrated with the following:

Go Modules branch

vendor directory

Vendor directory previously used for two purposes:

  • The exact version can be used to build dependencies.
  • Even if the original copy of the disappearance, but also to ensure these dependencies are available.

The module now have better mechanisms to achieve these two objectives:

  • By specifying the exact version dependencies in go.mod file.
  • Availability by caching proxy ( $GOPROXY) implementation.

And vendor directory is also difficult to manage these dependencies, with the passage of time will fall into the black hole node_modules the same dilemma.

node_modules black hole

Therefore, the use Go Modules by default completely ignore dependencies vendor, but to over-smooth, you can use the go mod vendorcommand to create a vendor directory.

Go in and use the command-line tool -mod=vendorparameters, such as: go test -mod=vendor ./...; or set the environment variable GOFLAGS is -mod=vendor, it will assume vendor directory that contains a copy of the correct dependencies and ignore the dependencies described go.mod file to build.

Environment Variables GOPROXY

Set the environment variable GOPROXY can solve the problem in mainland China can not go get the use of:

The export GOPROXY=https://goproxy.iocan write Shell profile.

Common Commands

  • go mod init: Create a new module, go.mod initialization file parameters for the import path to the module, it is recommended to use this form. Such go mod init github.com/linehk/exampleas: .
  • go get: Change the dependencies version (or add a new dependency).
  • go build, go testAnd other command: Go command-line tool to add new dependencies as needed. Such as: go test ./...test the current module.
  • go list -m all: Print module dependencies.
  • go mod tidy: Removing unused dependencies.
  • go list -m -versions github.com/gin-gonic/gin: Lists all versions of the module.
  • go mod verify: Verify hashes.

Integration with GoLand

Go Modules in use in GoLand 2019.1.3 requires two settings:

  1. Preferences -> Go -> Go Modules (vgo), check the Enable Go Modules (vgo) integration to enable Go Modules, and Proxy input box https://goproxy.io. as the picture shows:
    Use Go Modules in GoLand set 1
  2. Preferences -> Go -> GOPATH, check the Index entire GOPATH to index the entire GOPATH, or can not import the package. as the picture shows:
    Go Modules used in set 2 GoLand

After set as above, can be introduced when the packet is not in the cache, Sync packages of ... click to download the package:

Download Package

Reference links

Command go

Go & Versioning

Semantic Version 2.0.0

Reproduced in: https: //juejin.im/post/5d0b865c6fb9a07f050a6f45

Guess you like

Origin blog.csdn.net/weixin_34051201/article/details/93172787