# Errors are values. Go 1.20 made them a list.

> Go 1.20 shipped on the first of February with errors.Join and multiple %w verbs. A small change that finally made the error model click for someone arriving from C# and TypeScript - plus a preview of profile-guided optimisation I would measure before believing.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2023-02-25
- Tags: Go, C#, TypeScript, Error Handling, Performance
- URL: https://deyanpeev.com/posts/go-errors-became-a-list

I keep picking Go up and putting it down. Every time I come back the same bit
feels foreign, and I'd never stayed with it long enough to stop translating it
into something else.

Go 1.20 landed on the first of February, and one of its smaller additions is
what finally made me stop translating.

# Coming from C# and TypeScript

In C# and TypeScript, failure travels on a second, invisible return path. A
function's signature tells you what it produces when it works and says nothing
about what it does when it doesn't. You find that out from the documentation,
or from a stack trace at three in the morning, or never.

Go puts it in the signature:

```go
func loadConfig(path string) (Config, error)
```

Everybody's first complaint is the verbosity, `if err != nil` on every third
line. Mine was the same, and I think it is the wrong complaint. The verbosity is
a symptom. The difference underneath is that there's one way out of a function
and you can see it at the call site. Nothing unwinds past you while you aren't
looking.

What I did miss was structure. An exception carries a type you can catch on and
an inner exception underneath it. Go's answer to that arrived in 1.13. You wrap
with `%w`, and then you interrogate the chain.

```go
if err != nil {
    return fmt.Errorf("loading config from %s: %w", path, err)
}
```

`errors.Is` walks that chain looking for a particular value, `errors.As` walks it
looking for a particular type. It's a linked list, and the caller can ask
questions of any link in it.

# What 1.20 added

A chain has one link per level, and plenty of real failures don't look like
that.

Validating ten fields on a request produces up to ten independent problems, and
the user would like to see all of them. Closing three resources in a `defer` can
fail three seperate ways. Or you fan out to five services and gather the
results, and get up to five failures with no ordering between them.

Until 1.20 you either returned the first failure and threw away the rest, or you
wrote a multi-error type. Nearly every codebase had its own, slightly different
from the next, and they didn't work with anybody else's helpers.

1.20 adds `errors.Join`, and lets `fmt.Errorf` take more than one `%w`:

```go
func (c *Client) Close() error {
    return errors.Join(
        c.conn.Close(),
        c.cache.Close(),
        c.metrics.Flush(),
    )
}
```

This is better than the type I would have written.

Nil arguments are discarded, and if every argument is nil the result is nil. So
the accumulate-and-return pattern needs no special casing at the end, the happy
path falls out of the same expression.

```go
var errs error
for _, field := range fields {
    errs = errors.Join(errs, validate(field))
}
return errs // nil if nothing failed
```

And the result still answers the standard questions. `errors.Is` and `errors.As`
now traverse a tree where they used to walk a list, so a caller checking for
one specific condition finds it whether it arrived alone or alongside four
others:

```go
if errors.Is(err, ErrNotFound) {
    // true even if err also wraps three unrelated failures
}
```

That replaces a lot of home-grown types with very little API.

One thing to know before it surprises you in a log aggregator. The joined
error's message is the individual messages separated by newlines, not commas, so
a single "error" field in structured logging suddenly spans several lines.

# The other headline, which I would measure first

1.20 also ships a preview of profile-guided optimisation. You collect a CPU
profile from a real workload, hand it back to the compiler at build time, and it
uses the profile to make application-specific decisions, mostly about what to
inline.

The figure in the announcement is around three to four per cent on typical
applications, and the release notes are explicit that this is a preview with
rough edges that may rule out production use.

I have written before about the difference between a published percentage and a
number you can put in a capacity plan, and this is same situation again. Three
to four per cent is a real improvement. It's also well inside the range where an
unrepresentative benchmark will tell you anything you want to hear. If I wanted
it I'd build both ways, run the same load against both, and keep tail latency as
well as the mean. And I would not build a plan on a preview.

The rest of the release is useful in smaller ways. You can convert a slice to an
array without the pointer dance, build speeds are back in line with 1.17 after a
couple of releases of drift, and coverage collection now extends to whole
binaries, so integration tests can contribute to a coverage number (until now
they were invisible to it).

# Why I keep coming back

Go's release cadence is six months and boring, and I've come to appreciate that
more the longer I work. Nothing in 1.20 asks me to restructure anything.
`errors.Join` is additive, PGO is opt-in, and code written against 1.19 keeps
compiling.

I don't write Go professionally. It is still a language I use for weekend
things, and I read more of it than I write. But I keep returning because what
it's strict about (a function can fail, the signature says so, the call site
handles it) is what breaks in the systems I work on.
