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...