GO environment variable

There are a lot of packages that need to be introduced in Go, just like any other language. To consolidate your knowledge of package management for the Go language, start with the installation environment variables. Here only say Win10 installation, Win10 install Go language after the completion of the installation Go will automatically set two environment variables GOROOT and GOPATH, if you delete the second installation will need to configure environment variables. There are three environment variables, GOROOT, GOPATH, and GOBIN. GOROOT: Go language installation directory GOPATH: self-defined working directory, workspace directory, can have multiple executable files generated by GOBIN: Go program path

It can be used after the initial installationgo versionSee if the first two environment variables are installed:

Next use go env to look at the relevant environment variables:

GOAPATH

GOBIN can be ignored for now. It is not important to note that GOPATH defines a workspace. After setting up this workspace, we usually create three new folders under this workspace: SRC, PKG, bin. SRC: store go source file, self-written file; PKG: used to store archive files generated after compilation. Bin: stores the compiled executable file.

GO Modules

However, there are some drawbacks to using GOPATH for project development: there is no guarantee that multiple projects will have different version numbers under one workspace. Without the concept of version control, you can’t specify a third-party package version number for your current project, hence the Go Module mode in Go1.13+. It is not recommended to create a project using Go Modules under GoPATH/ SRC. Use Go mod -w GO111MODULE=on separately from GoPATH. Auto mode enables Go Modeules whenever a project contains the Go. mod file.

GOPATH is useless after this mode is turned on. Go ignores the GOPATH and Vendor folders and only downloads dependencies according to go.mod. After this mode is turned on, a new project will need to use itGo mod init Module nameGenerate the go.mod file in the project root directory, which records the name and version of the dependent package, regardless of GOPATH.

In addition to setting GO111MODULE, GOPROXY also needs to be set. For some reasons, many packages cannot be downloaded in China. Just like other languages, it is necessary to switch the source address (download source) for downloading GO packages. Using the go env – w GOPROXY = https://goproxy.cn, direct, and then use the go mod download * * * * * from domestic stand downloaded to the local image.

Go mod

Check out other common go mod commands:

    1. Go Mod Tidy project dependencies delete extra downloads without
    1. Go mod vendor generates the vendor directory and imports dependencies, which will transfer the dependency package to the vendor folder. Remember to ignore this folder in the.gitignore file when using Git
    1. Go clean – Modcache cleans mod dependencies
    1. Go mod Verify verifies that the dependency is correct
    1. Go get github.com/ Project address/package name @version Updates to a version

You can also use go mod help to view the go mod command help.

Next talk about GO language package management knowledge.