What you need to pay attention to about the commands “go get” and “go install” in Go 1.16

By | December 23, 2020

Go 1.16 contains many Modules-related updates, and you can directly view its Release Note for details. Overall, it contains the following points:

  • Go111module is On by default. If you want to restore the previous behavior, you need to set Go111module to Auto, which almost means that the GOPATH mode will gradually fade out of people's vision;
  • The go install command can accept a version suffix (for example, go install sigs.k8s.io/[email protected]), and it runs in a module-aware mode and can ignore Go.mod in the current directory or the upper directory file. This is very convenient for installing binary without affecting the main module dependency;
  • In the future, go install is designed to be "used to build and install binary files," go get is designed to be "used to edit go.mod to change dependencies", and when used, it should be shared with the -d parameter. In future versions -d may be enabled by default;
  • Go build and go test no longer modify Go.mod and Go.sum by default. It can be done through go mod tidy, go get, or manually;

In summary, what must be noted about go install and go get are:

  • Basically go install <package>@<version> is a global installation for commands: For example: go install sigs.k8s.io/[email protected];
  • The function of go get to install the binary, the subsequent version will be deleted;
  • go get is mainly designed to modify go.mod to add dependencies, but there are also commands like go mod tidy, so the frequency of use may not be very high;

Tool installation issues resolved in Go 1.16
So far, Go has been using the go get command to install the tools we need into the $GOPATH/bin directory, but this approach has a serious problem. Go get has the ability to change the go.mod file, so we must avoid letting it touch our go.mod file when executing the go get command, otherwise it will use the tool we installed as a dependency.

About go get and go.mod

go get transfers all functions related to binary installation to go install, which only exists as a command for editing the go.mod file. In the subsequent version (planned to be Go 1.17), the function of installing the binary of go get will be deleted, and the behavior of go get will be equivalent to that we execute the go get -d command now, just download the source code and add the dependency to go. mod can be.

How to edit go.mod
In Go 1.16, another behavior change is that go build and go test will not automatically edit go.mod. Based on the above information, the following processing will be performed in Go 1.16: