Go 1.9 Release Party

Your Meetup

25 August 2017

Gopher

License and Materials

This presentation is licensed under the Creative Commons Attribution-ShareAlike 4.0 International licence.

The materials for this presentation are available on GitHub:

You are encouraged to remix, transform, or build upon the material, providing you distribute your contributions under the same license.

If you have suggestions or corrections to this presentation, please raise an issue on the GitHub project.

2

Go 1.9

Go 1.9 is released 🎉

Go 1.9 is the 10th release in the Go 1 series. It follows from the previous version, Go 1.8, released on the 16th of February, 2017

3

What's happened in the last six months?

What's changed?

4

Language changes

5

Type Aliases

Go now supports type aliases to support gradual code repair while moving a type between packages.

In short, a type alias declaration has the form:

type T1 = T2

This declaration introduces an alias name T1—an alternate spelling—for the type denoted by T2; that is, both T1 and T2 denote the same type.

The type alias design document and an article on re-factoring cover the problem in detail.

6

Ports

No new ports were added during 1.9, however there have been some changes to the support platforms.

7

Performance

8

Performance

As always, the changes are so general and varied that precise statements about performance are difficult to make.

Most programs should run a bit faster, due to speedups in the garbage collector and optimizations in the standard library.

Do you have a Go 1.9 performance story to tell? Blog it, and I'll retweet the crap out of it.

9

Garbage collector specific

10

Toolchain improvements

11

Parallel Compilation

The go tool has always compiled runtime.NumCPUs() packages in parallel.

With Go 1.9, inside a single package functions are now compiled in parallel.

Depending on the width and height of your dependency tree, and the number of cores available, this could give no speed up, or a measurable improvement.

export GO19CONCURRENTCOMPILATION=0

disables this behaviour.

12

./... no longer matches vendor/...

No more

go test $(go list ./... | grep -v vendor)

shenanigans.

If you do want to test your code under vendor/, you can use something like

go test ./vendor/...
13

Default $GOROOT

The go tool will now use the path from which it was invoked to attempt to locate the root of the Go install tree.

This means that if the entire Go installation is moved to a new location, the go tool should continue to work as usual.

This is one less reason to need to explicitly set $GOROOT.

Note: this does not affect the result of the runtime.GOROOT function, which will continue to report the original installation location; this may be fixed in later releases.

14

go env -json

The new go env -json flag enables JSON output, instead of the default OS-specific output format.

% go env -json 
{
       "CC": "gcc",
       "CGO_CFLAGS": "-g -O2",
       "CGO_CPPFLAGS": "",
       "CGO_CXXFLAGS": "-g -O2",
       "CGO_ENABLED": "1",
       "CGO_FFLAGS": "-g -O2",
       "CGO_LDFLAGS": "-g -O2",
       "CXX": "g++",
       "GCCGO": "gccgo",
       "GOARCH": "amd64",
       "GOGCCFLAGS": "-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build254210362=/tmp/go-build -gno-record-gcc-switches",
       "GOHOSTARCH": "amd64",
       "GOHOSTOS": "linux",
       "GOOS": "linux",
       "GOPATH": "/home/dfc",
       "GOROOT": "/home/dfc/go",
       "GOTOOLDIR": "/home/dfc/go/pkg/tool/linux_amd64"
}
15

go test -list

The go test command accepts a new -list flag, which takes a regular expression as an argument and prints to stdout the name of any tests, benchmarks, or examples that match it, without running them.

% go test -list Compare bytes
TestCompare
TestCompareIdenticalSlice
TestCompareBytes
BenchmarkBytesCompare
BenchmarkCompareBytesEqual
BenchmarkCompareBytesToNil
BenchmarkCompareBytesEmpty
BenchmarkCompareBytesIdentical
BenchmarkCompareBytesSameLength
BenchmarkCompareBytesDifferentLength
BenchmarkCompareBytesBigUnaligned
BenchmarkCompareBytesBig
BenchmarkCompareBytesBigIdentical
16

go tool pprof

pprof has received some love.

Profiles produced by the runtime/pprof package now include symbol information, so they can be viewed in go tool pprof without the binary that produced the profile.

% go test -test.run=xxx -test.bench=Max strings -test.cpuprofile=c.p
BenchmarkSingleMaxSkipping-4     2000000               912 ns/op        10964.09 MB/s
PASS
ok      strings 3.054s
% go tool pprof c.p

The go tool pprof command now uses the HTTP proxy information defined in the environment, using http.ProxyFromEnvironment.

17

Other toolchain improvements

18

Runtime changes

19

Mid-stack inlining

Inlining has historically been limited to leaf functions because of the concern of aggressive inlining on stack trace output.

Users of runtime.Callers should avoid directly inspecting the resulting PC slice and instead use runtime.CallersFrames to get a complete view of the call stack, or runtime.Caller to get information about a single caller. This is because an individual element of the PC slice cannot account for inlined frames or other nuances of the call stack.

Specifically, code that directly iterates over the PC slice and uses functions such as runtime.FuncForPC to resolve each PC individually will miss inlined frames. To get a complete view of the stack, such code should instead use CallersFrames.

Code that queries a single caller at a specific depth should use Caller rather than passing a slice of length 1 to Callers.

runtime.CallersFrames has been available since Go 1.7, so code can be updated prior to upgrading to Go 1.9.

20

runtime poller improvements

Go has used epoll/kqueue/poll/select for network sockets for years.

Reads/Writes to other file descriptors have traditionally consumed a thread during operation.

Ian Lance Taylor landed a refactor that broke out the runtime polling subsystem and extended to work for the rest of the os package.

The os package now uses the internal runtime poller for file I/O. This reduces the number of threads required for read/write operations on pipes, and it eliminates races when one goroutine closes a file while another is using the file for I/O.

21

Plugins

Not much has changed, still lots to do.

David Crawshaw gave a great talk about all the ways that Go code can be built to interact with other languages, and itself (plugins) at GopherCon this year.

22

Go one-line installer

Jess Frazelle and Chris Broadfoot have been working on a one line binary installer for Go.

The installer is designed to both install Go as well as do the initial configuration of setting up the right environment variables and paths.

Personally, I'm a little disapointed they didn't name it upgoer.

23

Changes to the standard library

24

Transparent Monotonic Time support

The time package now transparently tracks monotonic time in each Time value, making computing durations between two Time values a safe operation in the presence of wall clock adjustments.

If a Time value has a monotonic clock reading, its string representation (as returned by String) now includes a final field "m=±value", where value is the monotonic clock reading formatted as a decimal number of seconds.

The new methods Duration.Round and Duration.Truncate handle rounding and truncating durations to multiples of a given duration.

25

time.String (cont.)

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()
    fmt.Println(t.String())
}

Just as you shouldn't compare t1 == t2 because they may be in a different timezone, you shouldn't also compare t1.String() == t2.String().

26

sync.Map

The sync package has a new type, sync.Map

sync.Map is a concurrent map with amortized-constant-time loads, stores, and deletes. It is safe for multiple goroutines to call a Map's methods concurrently.

sync.Map is not a general purpose replacement for a sync.Mutex / RWMutex and the built in map type.

27

math/bits

As an experiment in addressing the needs of low level crypto and bit twiddling needs of package writers, Go 1.9 includes a new package, math/bits.

math/bits contains functions to operate on values representing bit shifts, rotates, masks, and counts.

Where implemented by SSA backends, the math/bits functions are replaced by a native sequence of instructions. When no specific instruction exists, or is not implemented, the compiler treats the math/bits package as normal Go code.

package main

import (
	"fmt"
	"math/bits"
)

func main() {
    a := uint8(0x88)
    b := bits.RotateLeft8(a, 2)
    fmt.Printf("a: %8.b\nb: %8.b", a, b)
}
28

testing.Helper()

The new (*T).Helper and (*B).Helper methods mark the calling function as a test helper function. When printing file and line information, that function will be skipped. This permits writing test helper functions while still having useful line numbers for users.

Use it to exclude testing helpers from t.Errorf() and t.Fatalf() tracebacks.

package main

import (
	"errors"
	"regexp"
	"testing"
)

func Something() error { return errors.New("oops") }

func TestSomething(t *testing.T) {
    err := Something()
    checkErr(t, err) // line 16
}

func checkErr(t *testing.T, err error) {
    // t.Helper()
    if err != nil {
        t.Fatal(err) // line 22
    }
}

var tests = []testing.InternalTest{
	{"TestSomething", TestSomething},
}

var benchmarks = []testing.InternalBenchmark{}

var examples = []testing.InternalExample{}

var matchPat string
var matchRe *regexp.Regexp

func matchString(pat, str string) (result bool, err error) {
	if matchRe == nil || matchPat != pat {
		matchPat = pat
		matchRe, err = regexp.Compile(matchPat)
		if err != nil {
			return
		}
	}
	return matchRe.MatchString(str), nil
}

func main() {
	testing.Main(matchString, tests, benchmarks, examples)
}
29

And much more ...

30

dep

The official experiment.

Try it, use it, start making releases, start tagging your releases.

31

Go 1.next

The next release of Go will be ... wait for it ... Go 1.10.

32

Go.future

A GopherCon in July Russ Cox

"The conversation for Go 2 starts today, and it's one that will happen in the open, in public forums like the mailing list and the issue tracker. Please help us at every step along the way.

"Today, what we need most is experience reports. Please tell us how Go is working for you, and more importantly not working for you. Write a blog post, include real examples, concrete detail, and real experience. And link it on our wiki page. That's how we'll start talking about what we, the Go community, might want to change about Go.

Russ Cox
33

Conclusion

image credit Renee French

Upgrade to Go 1.9, now!

I know I said this last time, but it's still true that Go 1.9 is literally the best version of Go so far.

34

Thank you

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)