Created
July 9, 2019 18:13
-
-
Save ianfoo/3a4df545e9e9d9cb8c9af947c873edc3 to your computer and use it in GitHub Desktop.
Testing errors.Unwrap in go1.13 beta
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "errors" | |
| "fmt" | |
| ) | |
| func main() { | |
| errs := []error{ | |
| errors.New("errors.New error"), | |
| fmt.Errorf("fmt.Errorf error (no wrapping)"), | |
| fmt.Errorf( | |
| "fmt.Errorf wrapping errors.New error: %w", | |
| errors.New("base errors.New error")), | |
| fmt.Errorf( | |
| "fmt.Errorf wrapping fmt.Errorf error: %w", | |
| fmt.Errorf("base fmt.Errorf error")), | |
| fmt.Errorf( | |
| "fmt.Errorf wrapping fmt.Errorf wrapping errors.New: %w", | |
| fmt.Errorf("middle error: %w", errors.New("base error"))), | |
| } | |
| for _, err := range errs { | |
| fmt.Printf("before: %v\nunwrapped: %v\n\n", err, errors.Unwrap(err)) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ ./go1.13 run errtest.go | |
| before: errors.New error | |
| unwrapped: <nil> | |
| before: fmt.Errorf error (no wrapping) | |
| unwrapped: <nil> | |
| before: fmt.Errorf wrapping errors.New error: base errors.New error | |
| unwrapped: base errors.New error | |
| before: fmt.Errorf wrapping fmt.Errorf error: base fmt.Errorf error | |
| unwrapped: base fmt.Errorf error | |
| before: fmt.Errorf wrapping fmt.Errorf wrapping errors.New: middle error: base error | |
| unwrapped: middle error: base error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment