Why dependency management

Initially, the third-party package libraries Golang relied on were stored in the GOPATH directory. This leads to the problem that only one version of the same third-party library can be stored in a Golang environment

Problems can occur if different projects rely on different versions of the same third-party library. So go Module came into being after Golang 1.11 and became the default dependency management tool for the language in Golang 1.13

GO111MODULE

You can enable or disable the Go Module tool by setting GO111MODULE.

  • GO111MODULE=off — Disable go Module, the program is automatically compiled from GOPATH, vendor folder to find packages;
  • GO111MODULE=on — Start the go Module, ignore the GOPATH and vendor folders when compiling the program, and only download the dependencies according to go.mod;
  • GO111MODULE=auto — Enable go Module if the project does not exist in GOPATH/ SRC and the project root directory contains the go.mod file.

In Windows, run the following command to start GO111MODULE:

set GO111MODULE=on

Run the following command to start GO111MODULE on MacOS or Linux:

export GO111MODULE=on

By enabling the Go Module tool, you are not limited to building projects in the GOPATH/ SRC directory.

Off-topic: third party inventory placement

Golang will be stored in GOPATH/ PKG /mod, not in the vendor’s virtual environment directory specified by you, if you have learned Python or PHP.

Why are they all in GOPATH/ PKG /mod?

Since different projects rely on different versions of the same third-party library, it is certain that there will be different projects that rely on the same version of the same third-party library. In this case, I store and manage third-party libraries in a unified manner, so as to avoid the waste of hard disk space caused by storing the same version of third-party libraries in different project paths

As for the reason, I guess it’s the same as the gofMT unified code style — since different coding languages (formal issues) need to be unified (because of teamwork), the inventor can decide for you, you don’t have to worry about it.

The three inventors were probably gentle dictators. This is Also Golang’s philosophy of language — it’s all decided for you, you have no choice.

GOPROXY

The default value of Golang 1.13+ GOPROXY is proxy.golang.org. You are advised to set GOPROXY because it is inaccessible in China

Go env – w GOPROXY = GOPROXY. Cn, https://goproxy.io…

The go mod command is commonly used

Go mod init initializes the current folder, Create the go.mod file. Go mod Download Download rely on third-party libraries to log in to the local cache (default: GOPATH/ PKG /mod) go clean - modCache Clear the third-party library cache (default: GOPATH/ PKG /mod) go get github.com/gogf/gf@master download the main branch code of gf library go list-m -versions github.com/gogf/gf Go get./... Go mod Tidy adds missing Module, Delete useless Module Go mod vendor and copy the dependencies to Go Mod Graph under Vendor to print the module requirements graph in text modeCopy the code