Jonathan Amsterdam | e083197 | 2019-03-12 11:07:31 -0400 | [diff] [blame^] | 1 | # Error Values: Frequently Asked Questions |
| 2 | |
| 3 | The Go 2 [error values proposal](https://go.googlesource.com/proposal/+/master/design/29934-error-values.md) adds functionality to the [`errors`](https://tip.golang.org/pkg/errors) and [`fmt`](https://tip.golang.org/pkg/fmt) packages of the standard library for Go 1.13. There is also a compatibility package, [`golang.org/x/xerrors`](https://godoc.org/golang.org/x/xerrors), for earlier Go versions. |
| 4 | |
| 5 | 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. |
| 6 | |
| 7 | ## How should I change my error-handling code for Go 1.13? |
| 8 | |
| 9 | You need to be prepared that errors you get may be wrapped. |
| 10 | |
| 11 | - If you currently compare errors using `==`, use `xerrors.Is` instead. Example: |
| 12 | ``` |
| 13 | if err == io.ErrUnexpectedEOF |
| 14 | ``` |
| 15 | becomes |
| 16 | ``` |
| 17 | if xerrors.Is(err, io.ErrUnexpectedEOF) |
| 18 | ``` |
| 19 | |
| 20 | - Checks of the form `if err == nil` need not be changed. |
| 21 | |
| 22 | - Comparisons to `io.EOF` need not be changed, because `io.EOF` should never be wrapped. |
| 23 | |