The pkg/errors package is a very useful tool. It makes sense to wrap 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, consider the following code:
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 whether an error is nil first, before simply returning it. This recipe demonstrates how to use the package to wrap and unwrap errors, as well as basic stack trace functionality. The documentation for the package also provides some other useful examples such as printing partial stacks. Dave Cheney, the author of this library, has also written a number of helpful blogs and given talks on the subject; you can go to https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully to find out more.