The use of package management has been criticized by developers in Go 1.11
the past . Since GOPATH
this kind of package management has caused a lot of criticism from front-line developers, the Go
official sympathizes with the emotions of front-line developers on GOPATH
this kind of package management, and has been committed to providing a package management solution that is friendly to first-line developers. From the initial GOPATH
to GO VENDOR
the latest GO MODULES
, during this period, Go officials inevitably took a lot of detours, but what we have shown in front of us GO MODULES
is already a very decent module management.
Enter go env
the command , you can see that there is an extra GO111MODULE
variable, 111
which is Go 1.11
proposed on behalf of the version.
It has three optional values: off
, on
, auto
, and the default value is auto
.
GO111MODULE=off
Disable module support, packages will be looked up fromGOPATH
andvendor
folders when compiling.GO111MODULE=on
GOPATH
Enable module support, and foldersvendor
will be ignored when compiling , and only dependencies will be downloaded according to .go.mod
GO111MODULE=auto
, when the project$GOPATH/src
is outside and there arego.mod
files in the project root directory, module support is automatically enabled.
GO111MODULE
After Go 1.13
the version is opened by default.
go mod dependency management
go mod
There is no longer a dependency $GOPATH
so that it is not necessary $GOPATH
to create Go projects in the directory.
Create folders and main.go
files in this directory in turn.
Use the following command to automatically generate go.mod
the file.
go mod init github.com/zhenqi/module-main
Edit main.go
the file and enter the following content.
package main
import (
log "github.com/sirupsen/logrus"
)
func main(){
log.WithFields(log.Fields{
"animal": "walrus"
}).Info("A walrus appears")
}
Due to the use of third-party dependent libraries, you need to download the dependent libraries first.
go get github.com/sirupsen/logrus
Executing this statement will report an error.
This is because in Go 1.13
later versions, GOPROXY
the default value is proxy.golang.org
, unfortunately, this address cannot be accessed in China! So what to do? Fortunately, major domestic manufacturers provide mirror images, which are launched by Qiniuyun goproxy.cn
, so that domestic developers can use them better GO MODULES
.
go env -w GOPROXY=https://goproxy.cn,direct
Run go get github.com/sirupsen/logrus
the command again, and you can see that third-party dependencies are being downloaded.
Open go.mod
the file and obviously append a few lines.