Go (Golang) has released version 1.16 Beta1 on The 18th, and the main functions of golang have been finalized. I think most people are looking at the Go support on the Apple M1, and even the official Go blog has a special note on Go on ARM and Beyond to explain the Go support.

I’m going to skip the fun and talk about what you need to know about Go Get and Go Install in Go 1.16.

Currently Docker official image has not been released, I built a local image to use.

(MoeLove) ➜  go version
go version go1.16beta1 linux/amd64
Copy the code

An overview of

Go 1.16 includes a number of Modules related updates, which can be found directly in the Release Note. Overall, it contains the following key points:

  • GO111MODULEThe default isonIf you want to return to the previous behavior, you need to change theGO111MODULESet toautoThis almost means that the GOPATH model is going out of sight;
  • go installThe command can accept a version suffix, (for example, go install sigs.k8s.io/[email protected]), and it runs in mode-aware mode, ignoring the current or upper directorygo.modFile. This is handy for installing binaries without affecting the main module dependencies;
  • In the future,go installDesigned “for building and installing binaries”,go getIs designed “for editing go.mod change dependencies” and should be used with-dParameter sharing, in future versions-dMay be enabled by default;
  • go buildgo testBy default, no change is requiredgo.modgo.sum. throughgo mod tidygo getOr manually;

In summary, there are some things to note about Go Install and Go Get:

  • Basically, go install @

    is used for global installation of commands:

  • Go get installs binary features, which will be removed in later versions;

  • Go get is mainly designed to modify go.mod append dependencies and things like that, but there are also commands like Go mod Tidy, so they may not be used very often;

Resolved tool installation problems in Go 1.16

So far, Go has used the Go get command to install the tools we need into the $GOPATH/bin directory, but there is a serious problem with this approach. Go get has the ability to change the go.mod file, so we have to avoid the go get command touching our go.mod file, otherwise it will use our installed tools as a dependency.

Current solutions are usually:

➜ (MoeLove)cd$(mktemp -d); GO111MODULE = on the go a get sigs. K8s. IO/[email protected]Copy the code

Since 1.16, we can use the following method directly:

(MoeLove) ➜ go install sigs.k8s. IO /[email protected]Copy the code

It’s very simple and intuitive. Note that go install @

has been added since 1.16. Regardless of whether you are currently in a module, this command will install the specified version of the tool under $GOPATH/bin.

Also, since GO111MODULE is open by default in Go 1.16, Go Install won’t modify files like go.mod without causing any surprises.

Note:

@version Only the main software package can be installed. Non-main packages are not subject to this format.

About without@versiongo install

  • Outside the module, no@versionIs unable to install, will have the following error:
(MoeLove) ➜ go install -v sigs.k8s. IO /kind go install: Version is required when current directory is notin a module
        Try 'go install sigs.k8s.io/kind@latest' to install the latest version
Copy the code
  • If you are in the module directory, and you don’t carry@versionIf you perform installation, you can only installgo.modThe version that is already included in the. And cannot install without appearing ingo.modIn the package.
(MoeLove) ➜ mkdir -p/go/src/github.com/moelove/iris ➜ (MoeLove)cd /go/src/github.com/moelove/iris
Initialize the module(MoeLove) ➜ / go/src/github.com/moelove/iris go mod init go: creating new go. Mod: The module github.com/moelove/iris (MoeLove) ➜ / go/src/github.com/moelove/iris cat go. Mod module github.com/moelove/iris go 1.16Cannot install without @version(MoeLove) ➜ / go/src/github.com/moelove/iris go install - v sigs. K8s. IO/kind no required module provides package sigs.k8s.io/kind; try'go get -d sigs.k8s.io/kind' to add it

# Use go get -d to download(MoeLove) ➜ / go/src/github.com/moelove/iris go get - d sigs. K8s. IO/kind go get: added sigs. K8s. IO/kind v0.9.0You can see that it has been added to the module dependency(MoeLove) ➜ / go/src/github.com/moelove/iris cat go. Mod module github.com/moelove/iris go 1.16 the require sigs. K8s. IO/kind V0.9.0 / / indirect# Delete the local Kind tool➜ / go/src/github.com/moelove/iris (MoeLove)whichKind/go/bin/kind (MoeLove) ➜ go/src/github.com/moelove/iris rm/go/bin/kind ➜ (MoeLove) /go/src/github.com/moelove/iriswhich kind

Install without @version(MoeLove) ➜ / go/src/github.com/moelove/iris go install - v sigs. K8s. IO/kind ➜ / go/src/github.com/moelove/iris (MoeLove)whichKind/go/bin/kind (MoeLove) ➜ / go/src/github.com/moelove/iris kind version kind v0.9.0 go1.16 beta1 Linux/amd64Copy the code

aboutgo getgo.mod

Go get transfers binary installation-related functionality to Go Install and exists only as a command for editing go.mod files. In a later release (planned to be Go 1.17), the Go Get installation binary will be removed, and the Go Get behavior will now be the same as executing the Go get -d command. Simply download the source code and add the dependencies to go.mod.

go.modHow to edit

In Go 1.16, another behavior change is that Go Build and Go test will no longer automatically edit go.mod. Based on the above information, Go 1.16 will do the following:

  • Modify go.mod by modifying the import statement in the code:

    • go getCan be used to add new modules;
    • go mod tidyDelete useless modules.
  • Write an unimported module to go.mod:

    • go get <package>[@<version>];
    • go mod tidyCan also;
    • Manual editing;

What do I need to know about upgrading from 1.15?

Since Go Build and Go Test do not automatically edit Go.mod, the original behavior can be handled together with Go Mod Tidy.

conclusion

There are some incompatible changes to Go Install and Go Get in Go 1.16, but I’m looking forward to 1.16, which is much cleaner and less mentally taxing.


Please feel free to subscribe to my official account [MoeLove]