Error Handling
In Go, an error is a normal value.
Think of it as a sticky note that says what went wrong.
Functions often return an error as the last result.
You check it with if err != nil.
The error interface
The built-in error type is a small checklist with one method: Error() string.
Make simple errors with errors.New or fmt.Errorf.
Checking errors
Call the function. Then look at the error before using the other return values.
If you cannot fix it, return the error up to the caller.
nil means no sticky note. Everything is fine.
Wrapping errors
Use fmt.Errorf with %w to wrap an error with extra context.
Like putting a new sticky note on top of an old one.
The old note is still there under the new words.
errors.Is
Callers can unwrap later with errors.Is.
It asks: is this sticky note (or one under it) the same as ErrNotFound?
Go style for errors
Handle errors near where they happen.
Or return them with helpful context.
Do not ignore them with a blank _ unless you really mean it.
Handle errors near where they happen, or return them with helpful context.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.