Error Handling in Go

Error handling is important for even the most basic Go program. Errors in Go implement the Error interface and must be dealt with at every layer of the code. Go errors do not work like exceptions, and unhandled errors can cause enormous problems. You should strive to handle and consider errors whenever they occur.

This chapter also covers logging since it's common to log whenever an actual error occurs. We'll also investigate wrapping errors, so that the given error should provide an additional context as it's returned up the function stack, so that it's easier to determine the actual cause of certain errors.

In this chapter, the following recipes will be covered:

  • Handling errors and the Error interface
  • Using the pkg/errors ...

Get Go Programming Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.