Go 1.20 brought a lot of new features and improvements. In this post, I’d like to review the ones that caught my eye. This is by no means an exh

Cherry-Picked Features from Go 1.20

submited by
Style Pass
2023-02-07 14:30:10

Go 1.20 brought a lot of new features and improvements. In this post, I’d like to review the ones that caught my eye. This is by no means an exhaustive list; for that, see the official release notes.

The “errors as values” concept (as opposed to exceptions) has gained renewed popularity in modern languages such as Go and Rust. You know this well because it’s impossible to take a step without tripping over an error value in Go.

Now err is both errRaining and errWindy at the same time. The standard functions errors.Is() and errors.As() can work with this:

Starting from 1.20, we can create a context using context.WithCancelCause(). Then cancel() will take one parameter — the root cause of the error:

You may ask — why context.Cause()? It seems logical to add the Cause() method to the context itself, similar to the Err() method.

Sure. But Context is an interface. And any change to the interface breaks backward compatibility. That’s why it was done differently.

Leave a Comment