In Go it’s idiomatic to communicate errors via an explicit, separate return value. This contrasts with the exceptions used in languages like Java and Ruby and the overloaded single result / error value sometimes used in C. Go’s approach makes it easy to see which functions return errors and to handle them using the same language constructs employed for other, non-error tasks. See the documentation of the errors package and this blog post for additional details. |
|
|
|
|
|
By convention, errors are the last return value and
have type |
|
|
|
A |
|
A sentinel error is a predeclared variable that is used to signify a specific error condition. |
|
|
|
We can wrap errors with higher-level errors to add
context. The simplest way to do this is with the
|
|
|
|
It’s common to use an inline error check in the |
|
|
|
|
|
|
|
Next example: Custom Errors.