TIL What's New in Go 1.16 and how to use it in a Docker Container

 This'll be brief.

Go 1.16

Check out the release notes.
Some highlights:

Embed files directly into variables at compile time

The embed package lets you embed files directly into variables using a simple directive -- at compile time.  In the past, you'd have to declare a `var` and the use an `init()` function (or other function) to load the contents of a file into that var -- all of which happens at runtime and is subject to issues.  What's worse: if your application was a docker container, then you'd have had to make sure the final, runnable container had that file in there, too, by using a `COPY` directive.

Go install replaces Go get as a way to install CLIs

From the docs:

go install, with or without a version suffix (as described above), is now the recommended way to build and install packages in module mode. go get should be used with the -d flag to adjust the current module's dependencies without building packages, and use of go get to build and install packages is deprecated. In a future release, the -d flag will always be enabled.

What does this mean?  To quote a friend

in 1.15 if you ‘go get’ something, it modifies your go.mod.  And you couldn’t use go get with an @version consistently unless you were in a go module directory.  Now go install will download, compile, install binaries without touching your go.mod, and you can specify the version of the thing you want

 Using Go 1.16 in a Docker container

It's easy... the `golang:alpine` image already points to `1.16`.  See Docker Hub.

Comments