Get the latest tech news

Don't just check errors, handle them gracefully (2016)


This post is an extract from my presentation at the recent GoCon spring conference in Tokyo, Japan. Errors are just values I’ve spent a lot of time thinking about the best way to handle errors in Go programs.

If someone asks you to export an error value from your package, you should politely decline and instead suggest an alternative method, such as the ones I will discuss next. For example, interactions with the world outside your process, like network activity, require that the caller investigate the nature of the error to decide if it is reasonable to retry the operation. The key here is this logic can be implemented without importing the package that defines the error or indeed knowing anything about err ‘s underlying type–we’re simply interested in its behaviour.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of errors

errors

Related news:

News photo

Bloomberg's AI-Generated News Summaries Had At Least 36 Errors Since January

News photo

Google finally rolls out fix for 'Untrusted Device' errors on older Chromecasts

News photo

Discovering errors in Donald Knuth's TAOCP