About go get and go install from Go1.16

This article is a pinch hitter for the 16th day of Go Advent Calendar 2020. Strangely, I decided to talk about Go 1.16 on the 16th day.

Introduction

Go1.16 includes various updates for Modules. cf. https://tip.golang.org/doc/go1.16#tools

In this article, I will explain how to use go get / go install after Go1.16, and points to keep in mind. By the way, the following explanations are based on Module mode, not GOPATH mode. (Otherwise it would be too complicated)

Points to keep in mind

Tool installation issues resolved in Go 1.16

Until now, the familiar go get command has been used to install tools globally ($ GOPATH/bin) in Go, but it had one problem. Since go get also has the role of editing go.mod, to install only the tool without touching go.mod," go through the directory with go.mod" " I needed to enable go.mod on the tool side and do go get. It is as follows when summarized in one.

$ cd $(mktemp -d); GO111MODULE=on go get golang.org/x/tools/gopls

I don't want to spend such a lot of time on installing it, and above all, I did go get without knowing about Modules and unwillingly updated go.mod. (Source is the author)

So, from Go 1.16 it looks like this:

$ go install golang.org/x/tools/gopls@latest

It was very refreshing.

** Actually, this format go install <package> @ <version> is newly introduced in Go 1.16 **. This format consistently installs the specified version in $ GOPATH/bin, whether under Module or not.

In Go1.16, GO111MODULE is on by default, and go install no longer edits go.mod, so you don't have to worry about accidents.

Note

Note: About go install <package> without version specified

About other features of go install

Other features of go install, that is

As for, it is the same as before. These are things that you can live without, but it may be useful to remember them once in a while.

About go get, go.mod

On the other hand, go get transfers the role of binary installation to go install and is organized as a command only for editing go.mod. In releases after Go1.16 (scheduled for Go1.17), the function of binary installation by go get has been removed, and all go gets are equivalent to go get -d, and the source is downloaded and go. The behavior will be just to add to mod.

How to edit go.mod

Another change in Go1.16 was that go build and go test did not automatically edit go.mod. Based on these, the method of handling go.mod in Go1.16 is as follows.

A) How to write an import statement in your code and then reflect it in go.mod

B) How to add an unimported module to go.mod

I've listed four, but in reality, go mod tidy covers all of them, so it seems that you can go with just one go mod tidy.

Changes from Go 1.15

As mentioned earlier, the automatic editing feature of go.mod by go build etc. will be disabled from Go1.16, so users up to Go1.15 will have go.mod changed. But expecting go build, what? May be. Let's do go mod tidy.

Also, since Go1.17 or later will remove the function of binary installation of go get, it seems that various procedure manuals and scripts need to be modified by then.

gopls also has a change following Go1.16 from v0.6.0 that does not automatically edit go.mod. Regarding the sequential development flow, I think that it will be arranged for new Modules.

Summary

There are some incompatible changes around the tool, but Modules from Go 1.16 are much simpler and less addictive, so I think it's easier to recommend to beginners. As a writer who has a strong desire to spread Go, the less addictive you are, the easier it will be to do it, and the more motivated you will be to spread it.

Besides, I can't wait for Go 1.16, which is full of features such as embed and io/fs. The release is scheduled for February next year.

Recommended Posts

About go get and go install from Go1.16
Get TypeElement and TypeMirror from Class
Get "2-4, 7, 9" from [4, 7, 9, 2, 3]
Learn more about docker image and Dockerfile FROM
[Android] Get random keys and values from HashMap
About npm install --save
About Bean and DI
About classes and instances
About gets and gets.chomp
Install and configure Keycloak
About redirect and forward
About encapsulation and inheritance
About Serializable and serialVersionUID
Install buildah and container-selinux
About Java setters and getters. <Difference from object orientation>
Get video information from Nikorepo and throw it to Slack
[Kotlin] Get Java Constructor / Method from KFunction and call it
Get attributes and values from an XML file in Java
About for statement and if statement
About synchronized and Reentrant Lock
About Ruby hashes and symbols
[Java] About String and StringBuilder
About the same and equivalent
About classes and instances (evolution)
About pluck and ids methods
Difference between bundle and bundle install
Consideration about classes and instances
About Java Packages and imports
About Ruby and object model
About Ruby classes and instances
About instance variables and attr_ *
About self-introduction and common errors
Install the memcached plugin on MySQL and access it from Java
Steps to install samba on CentOS 8 and connect from Windows 10 Explorer
How to get and add data from Firebase Firestore in Ruby
[Java] How to convert from String to Path type and get the path