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 package and wrapping errors
  • Using the log package and understanding when to log errors
  • Structured logging with the apex and logrus packages
  • Logging with the context package
  • Using package-level global variables
  • Catching panics for long-running processes
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.15.205