go module

Foreword

go 1.5 introduced a vendor management project dependencies, but is vendor storage path underneath GOPATH, each additional dependent also can have its own vendor, usually mess up, even though the vendor management tools dep same level of management, but relatively GOPATH path can not escape. In addition, version management of each packet also appears primitive, and even some development will depend on the package download directly from github themselves down into the bottom GOPATH the vendor. go dependencies management is a consistent sore point developers criticized. So in the long-awaited, go 1.11 finally introduced a package dependencies go module project management, in addition to reliance on project management package GOPATH, defined the dependencies of version management.

 

definition

A module is a collection unit go related package version information. Accurate records must rely on information and recompile dependencies.

 

Examples from the start

Use go module is actually very easy to use, here I will give an example to illustrate.

Examples of environmental information go:

$ go version

go version go1.12.4 darwin/amd64

 

The following example is dependent github.com/sirupsen/logrus output line of the log. Creating a directory outside GOPATH mytest, and then create a main.go file, as follows:

 

package main

import (
    log "github.com/sirupsen/logrus"
)

func main() {
        // Add this line for logging filename and line number!
    log.SetReportCaller(true)

    log.Println("hello world")
}

carried out

go mod init mytest

In fact, I mytest is specified module name can be any name, but must be specified, otherwise it will error  go: can not determine module path for source directory.

Then go build will execute successfully compiled, and more file go.mod and go.sum two related module:

$ ls
go.mod    go.sum    main.go    mytest

$ cat go.mod 
module mytest

go 1.12

require github.com/sirupsen/logrus v1.4.2

$ cat go.sum 
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

As can be seen from Examples go.mod project file is stored in the package dependency information go.sum dependencies stored inside the check information. The main focus of the information go.mod. You can see, the version information dependencies if we do not specify, go build defaults will for us to pull the latest version of the dependency package.

It can be concluded, using the go module is divided into the following steps:

  • go mod init $ moduleName initialization module information.
  • go build or go test standards such as command dependencies automatically update information engineering.
  • If there is a need to use go get $ packageName @ $ version, for example, go get [email protected], go get foo @ master, go get foo @ e3702bed2, can also be modified directly or using go.mod go mod edit (later article will mentioned) to obtain the specific version dependencies.

These are the basic go module workflow has to meet the daily work process requirements, the following will explain in detail the other usage go module.

 

Detailed usage

So go module a total of how many games are played it? Run directly go mod will have the answer:

$ go mod
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.

Usage:

    go mod <command> [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

Use "go help mod <command>" for more information about a command.

Init front of which I've already mentioned, it is not repeated here.

 

download 

Download dependencies to the cache directory.

 

edit

Edited version go.mod provide command functions, such as go mod edit -fmt go.mod will format go.mod.

用法 go mod edit [flag] [go.mod]

Which flag options are:

  • -fmt file format go.mod
  • -require = $ package: @version add dependencies, it will cover the same dependence that already exists. Add rely more recommended go get, go get because of go.mod will update the file, edit only updates the file you specify go.mod.
  • -droprequire = $ package: @version remove the dependency
  • -replace = $ oldPackage = rely $ newPackage update already exist. Code coverage is usually used for private warehouse Total warehouse.  

Here I focus on that -replace option, because a situation often encountered in production is due to this and that we need to fork a private warehouse to change third party open source libraries, for example, there is a small brother made two logrus development github.com/gogap/logrus, this time you need to use third-party open source libraries github.com/sirupsen/logrus before github.com/gogap/logrus replaced as follows:

$ go mod edit -replace="github.com/sirupsen/logrus=github.com/gogap/[email protected]"
$ go build
go: finding github.com/gogap/logrus v0.8.2
go: downloading github.com/gogap/logrus v0.8.2
go: extracting github.com/gogap/logrus v0.8.2

$ cat go.mod 
module mytest

go 1.12

require github.com/sirupsen/logrus v1.4.2

replace github.com/sirupsen/logrus => github.com/gogap/logrus v0.8.2

 

graph

Display dependency (FIG).

$ go mod graph 
mytest github.com/sirupsen/logrus@v1.4.2
github.com/sirupsen/logrus@v1.4.2 github.com/davecgh/go-spew@v1.1.1
github.com/sirupsen/logrus@v1.4.2 github.com/konsorten/go-windows-terminal-sequences@v1.0.1
github.com/sirupsen/logrus@v1.4.2 github.com/pmezard/go-difflib@v1.0.0
github.com/sirupsen/logrus@v1.4.2 github.com/stretchr/objx@v0.1.1
github.com/sirupsen/logrus@v1.4.2 github.com/stretchr/testify@v1.2.2
github.com/sirupsen/logrus@v1.4.2 golang.org/x/sys@v0.0.0-20190422165155-953cdadca894

 

tidy

Increasing the missing packets and does not rely on removal of the package. Dependencies automatically to download and cache the next $ GOPATH / pkg / mod directory.

It should be noted, tidy package will automatically update the version-dependent, so if it is not built at the beginning of the project or try to use less tidy, try to go get precise control with new dependencies.

vendor

The dependencies are copied to the vendor under the directory. He said in front of so many surely you must have a doubt: go build the site when you need to pull dependencies, if I did not compile machine outside the network (not accessible github) how to do? vendor is to use such a case, the execution go mod vendor to vendor dependencies copied to the bottom, then compiled machine code to perform push go build -mod = vendor on the local development machine (outside the network). Example:

$ go mod vendor
$ ls
go.mod    go.sum    main.go    mytest    vendor
$ go build -mod=vendor

 

verify

Check dependencies

$ go mod verify
all modules verified

 

why

Why point out the dependencies. Is the difference between the graph, why only a particular interpretation of the dependencies, and the graph is given a complete dependency graph.

$ go mod why github.com/konsorten/go-windows-terminal-sequences
# github.com/konsorten/go-windows-terminal-sequences
mytest
github.com/sirupsen/logrus
github.com/konsorten/go-windows-terminal-sequences

 

to sum up

Through an example of a print log demonstrates the use of all go module, including the basic daily usage and comprehensive usage presentation. New dependencies update is recommended go get. Dependencies replacement is recommended go mod edit -replace. Limited time offers vendor solutions compiling machine network.

 

reference

https://github.com/golang/go/wiki/Modules

 

Guess you like

Origin www.cnblogs.com/makelu/p/11028329.html