-
Notifications
You must be signed in to change notification settings - Fork 17.8k
ErrorValueFAQ
Jonathan Amsterdam edited this page Mar 12, 2019
·
28 revisions
The Go 2 error values proposal adds functionality to the errors
and fmt
packages of the standard library for Go 1.13. There is also a compatibility package, golang.org/x/xerrors
, for earlier Go versions.
We suggest using the xerrors
package for backwards compatibility. When you no longer wish to support Go versions before 1.13, use the corresponding standard library functions.
You need to be prepared that errors you get may be wrapped.
-
If you currently compare errors using
==
, usexerrors.Is
instead. Example:if err == io.ErrUnexpectedEOF
becomes
if xerrors.Is(err, io.ErrUnexpectedEOF)
-
Checks of the form
if err == nil
need not be changed. -
Comparisons to
io.EOF
need not be changed, becauseio.EOF
should never be wrapped.