Glide works well, why use Modules
In Go 1.11, the official package management tool was added, called Go Modules. Before the Go mod appeared, the most commonly used package managers were government, glide, etc. These tools did not meet Go's expectations for package management, such as each download, such as off-wall download, such as dependence on GOPATH, such as multiple versions Control, etc. Then Go Modules appeared, it does not depend on GOPATH, it is only related to the project, you can specify an agent, you can easily control by version, get rid of the dependence of GOPATH, and laid the foundation for the future development of Go.
Use Go mod command to manage packages
command | description |
---|---|
go init | Initialize the mod under the current directory item |
go tidy | Pull dependent modules and remove unused modules |
go vendor | Copy dependencies to vendor |
go edit | Edit go.mod |
go verify | Verify correct dependencies |
In fact, basically using init and tidy is enough.
Set environment variables
GO111MODULE
has three values, off, on, auto, off and on are off and on, auto will judge whether to use the modules function according to whether there is a go.mod file in the current directory. No matter which mode is used, the module function does not search for dependent files in the GOPATH directory by default.
GOPROXY
sets up a proxy service, https://goproxy.io . You can also set up a proxy service yourself, and then set GOPROXY as the proxy server address.
vim ~ / .bash_profile
added two lines of configuration
export GO111MODULE = on
export GOPROXY = https: //goproxy.io
source ~/.bash_profile
Give a chestnut
Create project myproject
main.go
package main
import (
"github.com/satori/go.uuid"
"fmt"
)
func main() {
uid := uuid.NewV4()
fmt.Println(uid)
}
Execute Go mod command, init and tidy
go mod init
go: creating new go.mod: module myproject
go mod tidy
go: finding golang.org/x/tools latest
go: downloading golang.org/x/tools v0.0.0-20200415034506-5d8e1897c761
go: extracting golang.org/x/tools v0.0.0-20200415034506-5d8e1897c761
go: finding gopkg.in/check.v1 latest
go: downloading gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f
go: extracting gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f
go: finding github.com/niemeyer/pretty latest
go: downloading github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e
go: extracting github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e
go: downloading github.com/kr/text v0.1.0
go: extracting github.com/kr/text v0.1.0
Compile execution result
go build main.go
./main
6723138d-ab2c-4de6-b996-732362985548
You can see the main file go.mod generated by Go mod
cat go.mod
module myproject
go 1.13
require (
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/satori/go.uuid v1.2.0
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
)
Each package is followed by a version. If you want to switch branches, later versions can be arbitrarily switched to the required branch, such as
require (
github.com/niemeyer/pretty master
github.com/satori/go.uuid v1.2.0
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
)
You can also use local code to replace remote code branches. You can use the following
/data/www/go/src/go.uuid instead of the remote branch github.com/satori/go.uuid.
Add the following code to the last line of go.mod
replace github.com/satori/go.uuid => /data/www/go/src/go.uuid
The use of Go mod is not particularly simple.