go modules are golang 1.11 plus new features. Now 1.12 has been released, it is time to use up. Modules officially defined as:
Go module is a collection of related packets. modules is a unit exchange and the source code version control. go commands directly support the use of modules, including a recording and parsing dependency on other modules. replacing the old modules GOPATH based method to specify which source files used in a given build.
How to Use Modules?
- Golang upgrade to 1.11 (now 1.12 has been released, it is recommended to use 1.12)
- Set GO111MODULE
GO111MODULE
GO111MODULE There are three values: off, on, and auto (default value).
- GO111MODULE = off, go command line support module does not function, look for dependencies manner will use the old version of the kind referred to by their vendor directory or GOPATH mode.
- GO111MODULE = on, go command line using the modules, but that will not go GOPATH directory lookup.
- GO111MODULE = auto, default values, go to the command line will decide whether to enable the function module according to the current directory. In this case it can be divided into two situations:
- The current directory and the files in the directory that contains go.mod outside GOPATH / src
- The current file in the directory containing the following go.mod file.
When the feature is enabled modules, dependencies storage location was changed to $ GOPATH / pkg, allows the coexistence of multiple versions of the same package, and multiple projects can share the cache module.
go mod
golang go mod provides commands to manage package.
go mod the following command:
command | Explanation |
---|---|
download | download modules to local cache (download dependencies) |
edit | edit go.mod from tools or scripts(编辑go.mod) |
graph | print module requirement graph (FIG printing module dependency) |
init | initialize new module in current directory (in the current directory initialization mod) |
tidy | add missing and remove unused modules (pulling missing module, a module without removal) |
vendor | make vendored copy of dependencies (dependent copied to the Vendor) |
verify | verify dependencies have expected content (dependent verification is correct) |
why | explain why packages or modules are needed (to explain why the need to rely on) |
How to use in the project
-
Create a directory outside GOPATH directory and use the generated go.mod go mod init initialization file:
➜ ~ mkdir hello ➜ ~ cd hello ➜ hello go mod init hello go: creating new go.mod: module hello ➜ hello ls go.mod ➜ hello cat go.mod module hello go 1.12
After go.mod Once the file is created, its contents will be go toolchain full control. go toolchain will be in the various types of command execution, such go get, go build, go mod and other modifications and maintenance go.mod file.
go.mod provides a module, require, replace and exclude four commands
- module package statement specifies the name (path)
- require statements to specify dependencies module
- statement may alternatively replace dependencies module
- exclude statements can ignore dependencies module
-
Server.go create a new file, write the following code:
package main import ( "net/http" "github.com/labstack/echo" ) func main() { e := echo.New() e.GET("/", func(c echo.Context) error { return c.String(http.StatusOK, "Hello, World!") }) e.Logger.Fatal(e.Start(":1323")) }
Execution go run server.go run the code will find go mod will automatically find that rely on automatic download:
$ go run server.go go: finding github.com/labstack/echo v3.3.10+incompatible go: downloading github.com/labstack/echo v3.3.10+incompatible go: extracting github.com/labstack/echo v3.3.10+incompatible go: finding github.com/labstack/gommon/color latest go: finding github.com/labstack/gommon/log latest go: finding github.com/labstack/gommon v0.2.8 # 此处省略很多行 ... ____ __ / __/___/ / ___ / _// __/ _ \/ _ \ /___/\__/_//_/\___/ v3.3.10-dev High performance, minimalist Go web framework https://echo.labstack.com ____________________________________O/_______ O\ ⇨ http server started on [::]:1323
Now look go.mod content:
$ cat go.mod module hello go 1.12 require ( github.com/labstack/echo v3.3.10+incompatible // indirect github.com/labstack/gommon v0.2.8 // indirect github.com/mattn/go-colorable v0.1.1 // indirect github.com/mattn/go-isatty v0.0.7 // indirect github.com/valyala/fasttemplate v1.0.0 // indirect golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a // indirect )
Principles go module installation package is to first pull the latest release tag, if no tag is pulling the latest commit, see Modules official introduction. go go.sum will automatically generate a file to record the dependency tree:
$ cat go.sum github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg= github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s= github.com/labstack/gommon v0.2.8 h1:JvRqmeZcfrHC5u6uVleB4NxxNbzx6gpbJiQknDbKQu0= github.com/labstack/gommon v0.2.8/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFYA+wQNJ4= github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= ... 省略很多行
-
Execute the script again go run server.go has been checked and found to skip steps depend on the installation.
-
You can use the command-go list -m -u all can be upgraded later to check the package, use go get -u need-upgrade-package upgrade will depend on the new version of the update to go.mod * can also be used to upgrade all go get -u rely
go get upgrade
- Run go get -u will be upgraded to the latest version or minor revision (xyz, z is the revision number, y is the minor version number)
- Run go get -u = patch will be upgraded to the latest revision
- Run go get package @ version will be upgraded to the specified version number version
- If you have a version of the run go get change, then the file will also change go.mod
Use replace the replacement can not get a direct package
For some reason known, not all can successfully download package, for example: package under golang.org.
modules may be replaced by the corresponding library using github to replace go.mod instruction file, for example:
replace (
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a => github.com/golang/crypto v0.0.0-20190313024323-a1f597ede03a
)
or
replace golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a => github.com/golang/crypto v0.0.0-20190313024323-a1f597ede03a