Error Chains in Go 1.13

Error Chains in Go 1.13

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 it's 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 an 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.

If you liked it and want to know when I post more, be sure to subscribe and thank y'all again for your time!


Did you find this information useful? If so, consider heading over to my donation page and drop me some support.

Want to ask a question or just chat? Contact me here