Quick Guide: Go 1.20 Features

Emre Odabas
4 min readJan 24, 2023

--

Go 1.19 was released nearly six months ago, and the community has already released rc3 of Go 1.20. If you wonder what comes with earlier versions, you could check For Quick Guide: Go 1.19 Features
For the next version -> For Quick Guide: Go 1.21 Features

Go 1.20 will be released in February 2023. You could check the release state. Let’s look at what comes with Go 1.20.

Table of Contents

Wrapping Multiple Errors

With this feature, we could quickly wrap errors into a single error.

func Join(errs ...error) error

fmt.Errorf and errors package functions now support multiple error ability. You can see a sample usage of these functions below.

This code will return “one for all, all for one“ and ”err2 “.

^^^ go top ^^^

Skip Pattern

Go CLI has a new feature for skipping with patterns. This approach could simply be used like the one below.

You could skip the Integration tests.

This skip pattern is also useable for benchmark tests and code generation.

 go test -bench=. -v -skip=<pattern>

go generate -skip=<pattern>

^^^ go top ^^^

Context with Cancel

We could cancel the context with cause after the Go 1.20 release.

func WithCancelCause(parent Context) (ctx Context, cancel CancelCauseFunc)
//
ctx, cancel := context.WithCancelCause(parent)
cancel(myError)
ctx.Err() // returns context.Canceled
context.Cause(ctx) // returns myError

^^^ go top ^^^

Unsafe Package

As you may not know, the unsafe package provides facilities for low-level programming, including operations that violate the type system*. Even the package named for keeping a distance is becoming crucial for tuning performance under the hood.

The package ability is extended with SliceData , String and StringData functions. These functions construct and deconstruct slice and string values without depending on their exact representation.

^^^ go top ^^^

Comparable Interface

Comparable types are defined with the comparable interface that could be used for constraining a type parameter function. For example, you could write equals function for all comparable types.

Understanding which types are comparable is a little bit tricky. It is described in the Go documentation;

  • Boolean, numeric, string, pointer, and channel types are strictly comparable.
  • Struct types are strictly comparable if all their field types are strictly comparable.
  • Array types are strictly comparable if their array element types are strictly comparable.
  • Type parameters are strictly comparable if all types in their type set are strictly comparable.

^^^ go top ^^^

Profile-Guided Optimization

Go 1.20 offers a new profile-guided optimization for executable code blocks. By default, if the cost of inlining is higher than 80, it will not be inlined. With PGO, Go more aggressively call inline functions. This inlining optimization improves performance by about 3–4% on benchmarks**. This feature is a preview and will be enhanced in future releases.

^^^ go top ^^^

No!! Memory Arenas

Google claims they reduce their CPU usage by up to 15% with this feature. Even this proposal was on the Go 1.20 plan for a while. It is removed from the release plan. You could check this article if you are still interested in what it was.

Other Changes

  • The go command now disables cgo by default. This makes Go work better in some minimal container environments as well as on macOS, where pre-distributed package archives have not been used for cgo-based packages since Go 1.16
  • Garbage collector optimization that gives up to %2 CPU performance.
  • With this version, Go has built-in coverage ability for integration tests
  • go build and test no longer -i command
  • Go 1.20 is the last release for;
    - Windows 7, 8, Server 2008 & 2012
    - MacOS 10.13 High Sierra and 10.14 Mojave.

^^^ go top ^^^

--

--