About go get and go install in Go 1.16

  sonic0002        2020-12-26 00:26:58       13,390        0    

Go version 1.16 beta1 has been released on 18 Dec 2020, major features of Go 1.16 have been finalized with this beta release. Many people are discussing about the support of Apple M1, however, this post will not cover this topic. Instead the focus will be on go get and go install changes.

There are lots of changes related to modules in Go 1.16, the details can be found in the release note. Below are some of the key highlights.

  1. GO111MODULE is on by default, if wanna keep old behavior, needs to set it to auto. This means that GOPATH would gradually get away from us.
  2. go install can take a version number as suffix. e.g. go install sigs.k8s.io/kind@v0.9.0. And it is module aware which means it would ignore the go.mod in current directory or parent directory. This would make binary installation easier without changing dependencies stated in go.mod.
  3. In the future, go install will be used for binary tool build and installation while go get will be mainly for dependency update.
  4. go build and go test will not update go.mod and go.sum. To update them, can run go mod tidy or go get.

go get is being used to install binary tools in $GOPATH/bin until now, but it has a problem where it will update go.mod as well apart from installing the binary tool and make it as a dependency to the project which may not be needed. To prevent updating the go.mod, normally, one needs to run below:

cd $(mktemp -d); GO111MODULE=on go get sigs.k8s.io/kind@v0.9.0

 Since Go 1.16, the above command can be simplified as:

go install sigs.k8s.io/kind@v0.9.0

This looks pretty straightforward. In addition, it will not update go.mod as GO111MODULE is on by default. 

One thing to note about go install is that it would cause error if run it without version when run it outside a module.

go install  -v sigs.k8s.io/kind
go install: version is required when current directory is not in a module
        Try 'go install sigs.k8s.io/kind@latest' to install the latest version

If run go install without version in module directory, only versions specified in go.mod would be installed.

In Go 1.16, the function to install binary tool with go get has been deprecated and developers should use go install instead. The function to update go.mod remains. Starting from Go 1.17, the function to install binary tool will be removed from go get. And later go get will serve the sole purpose of go get -d as of now.

When migrating from Go 1.15 to Go 1.16, go mod tidy needs to be ran when doing go build and go test, otherwise go.mod would not be updated.

Reference: Go 1.16 中关于 go get å’Œ go install 你需要注意的地方

GOLANG  GO 1.16  GO INSTALL 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

When do you shutdown your laptop?