Modules Management in Go Language (Let's Go 34)

The use of package management has been criticized by developers in Go 1.11the past . Since GOPATHthis kind of package management has caused a lot of criticism from front-line developers, the Goofficial sympathizes with the emotions of front-line developers on GOPATHthis kind of package management, and has been committed to providing a package management solution that is friendly to first-line developers. From the initial GOPATHto GO VENDORthe latest GO MODULES, during this period, Go officials inevitably took a lot of detours, but what we have shown in front of us GO MODULESis already a very decent module management.

Enter go envthe command , you can see that there is an extra GO111MODULEvariable, 111which is Go 1.11proposed on behalf of the version.

It has three optional values: off, on, auto, and the default value is auto.

  1. GO111MODULE=offDisable module support, packages will be looked up from GOPATHand vendorfolders when compiling.
  2. GO111MODULE=onGOPATHEnable module support, and foldersvendor will be ignored when compiling , and only dependencies will be downloaded according to .go.mod
  3. GO111MODULE=auto, when the project $GOPATH/srcis outside and there are go.modfiles in the project root directory, module support is automatically enabled.

GO111MODULEAfter Go 1.13the version is opened by default.

insert image description here

go mod dependency management

go modThere is no longer a dependency $GOPATHso that it is not necessary $GOPATHto create Go projects in the directory.

Create folders and main.gofiles in this directory in turn.

insert image description here

insert image description here

Use the following command to automatically generate go.modthe file.

go mod init github.com/zhenqi/module-main

insert image description here

Edit main.gothe 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")
}

insert image description here

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.

insert image description here

This is because in Go 1.13later versions, GOPROXYthe 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

insert image description here

Run go get github.com/sirupsen/logrus the command again, and you can see that third-party dependencies are being downloaded.

insert image description here

Open go.modthe file and obviously append a few lines.

insert image description here

Guess you like

Origin blog.csdn.net/coco2d_x2014/article/details/127813764