June 2017
Intermediate to advanced
400 pages
10h 32m
English
The pkg/errors package is a very useful tool. It makes sense to wrap basically every returned error using this package to provide extra context in logging and error debugging. It's flexible enough to print the entire stack traces when an error occurs or to just add a prefix to your errors when printing them. It can also clean up code since a wrapped nil returns a nil value. For example:
func RetError() error{ err := ThisReturnsAnError() return errors.Wrap(err, "This only does something if err != nil")}
In some cases, this can save you from having to check if an error is nil first before simply returning it. This recipe demonstrated how to use the package to wrap and unwrap errors, as well as basic stack trace functionality. ...
Read now
Unlock full access