Error Chains in Go 1.13

Donald Feury
1 min readAug 6, 2020

--

Originally posted here

Go added a neat feature in 1.13 that they call error chains

Errors can now implement a method called Unwrap that should return another error. Typically, this is an error that occurred further down in the call stack.

It is very common in go to have errors get tossed further and further back up the call stack as functions resolve, until its finally handled properly. Error chains allow you to carry a series of errors all the way back up the call stack more easily.

They added two new functions in the errors package to interact with this concept, Is and As

Is takes a argument of an error and another error, returns true if any instance of the second error occurs in the first error's chain, and false otherwise.

As takes the same arguments, but sets the value of the second argument equal to the error that was found in the first error's chain if it was present.

Check out the video to see the concept in action.

That’s a wrap

If you liked it and want to know when I post more, be sure to subscribe and thank ya’ll again for your time!

--

--

Donald Feury
Donald Feury

Written by Donald Feury

Appalachian Boi, Technology Consultant, Content Creator

No responses yet