Go 1.14 and Beyond

What we know already

Andreas Linz—klingt.net

2020-02-21T19+16:11

Go 1.14

Embedding of overlapping interfaces

$ docker run -v $PWD:/app -w /app --rm -it golang:1.13 go run overlapping-interfaces.go
./overlapping-interfaces.go:10:2: duplicate method Close
$ docker run -v $PWD:/app -w /app --rm -it golang:1.14-rc go run overlapping-interfaces.go
works

GOINSECURE

  • instruct go command to skip certificate validation or accept HTTP
  • useful when developing a module proxy
  • GOINSECURE=localhost go get foo/bar

Vendoring

  • commands will default to using vendoring if
    • go.mod specifies Go 1.14
    • and a /vendor folder is present
  • previously -mod=vendor must have been passed
  • usage of modules can be enforced with -mod=mod

Test log output

  • output of go test -v (or t.Log) is now streamed instead of being presented at the end of all tests

Runtime Improvements

(almost) Zero Overhead defer

This release improves the performance of most uses of defer to incur almost zero overhead compared to calling the deferred function directly. As a result, defer can now be used in performance-critical code without overhead concerns.

More Efficient Page Allocator

The page allocator is more efficient and incurs significantly less lock contention at high values of GOMAXPROCS. This is most noticeable as lower latency and higher throughput for large allocations being done in parallel and at a high rate.

Discord should consider to reevaluate their switch from Go to Rust 😉.

New package hash/maphash

  • collision-resistant
  • not cryptographically secure
  • hash functions map arbitrary string/[]byte to 64-bit integers
  • useful for building custom maps
  • not safe for concurrent use by multiple goroutines (can be initialized with the same seed to get equal hashes)

Minor Library Changes

crypto/tls

  • SSL3 support removed
  • TLS1.3 can not be disabled using GODEBUG anymore
  • when multiple certificate chains are configured) the first compatible with the peer is selected automatically (e.g. for providing ECDSA and RSA certificates)

ioutil.TempDir

  • ioutil.TempDir allows to specify a naming pattern (like TempFile)
  • useful when cleaning up temporary stuff of an application (e.g. rm -rf /tmp/my-app-*)
$ docker run -v $PWD:/app -w /app --rm -it golang:1.14-rc go run predictable-tempdir.go
/tmp/584949244-my-app
/tmp/my-app-532238635

mime

  • default type of .js and .mjs files is now text/javascript
  • application/javascript deprecated (IETF draft)

net/http

  • Header.Values returns all values associated to a header key (Header.Value only returns the first one)
  • httptest/Server adds EnableHTTP2

testing

  • t.Cleanup allows to define a custom cleanup function that is run after each test
$ docker run -v $PWD:/app -w /app --rm -it golang:1.14-rc go test -v ./cleanup-test-artifacts_test.go
=== RUN   TestCleanup
    TestCleanup: cleanup-test-artifacts_test.go:15: removing  /tmp/golang-leipzig-631932423
--- PASS: TestCleanup (0.00s)
PASS

Go 1.15

  • proposals
  • scheduled for August 2020
  • no major changes

Int to String

  • cmd/vet warn about int to string conversion
  • just replace those ints with runes
  • example: string(10) is "\n" not "10"
  • unicode/utf8 provides runeToString (play)

Impossible interface-interface assertion

Currently, Go permits any type assertion x.(T) (and corresponding type switch case) where the type of x and T are interfaces.

  • example: Two methods with the same name buth different signature (play)
  • impossible conversion is known at compile-time
  • cmd/vet will warn about this (will be a compile error later)

Constant-evaluate index and slice expressions

Future

The primary goals for Go remain package and version management, better error handling support, and generics.

f, err := os.Open(filename)
if err != nil {
    return …, err  // zero values for other results, if any
}
// with try
f := try(os.Open(filename))

Generics

we are also making progress on the generics front (more on that later this year).

Questions? {data-background-image=https://images.unsplash.com/photo-1557318041-1ce374d55ebf?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80 data-background-opacity=0.31415