Created
May 23, 2020 06:47
-
-
Save dschowta/0bfc9f6c09f797f6d6b2e879687bdc4b to your computer and use it in GitHub Desktop.
experiment to show how erro unwrapping works in go:. Playground: https://play.golang.org/p/i0KKTRKfXOR
This file contains 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() { | |
//Wrapping the errors | |
err1 := errors.New("first") | |
err2 := fmt.Errorf("err2 %w", err1) | |
err3 := fmt.Errorf("err3 %w", err2) | |
//unwrapping the errors | |
gotErr1 := fmt.Errorf("hello %w", err3) | |
fmt.Println(gotErr1) | |
gotErr2 := errors.Unwrap(gotErr1) | |
fmt.Println(gotErr2) | |
gotErr3 := errors.Unwrap(gotErr2) | |
fmt.Println(gotErr3) | |
gotErr4 := errors.Unwrap(gotErr3) | |
fmt.Println(gotErr4) | |
//Unwrapping the inner most error keeps returning the same error | |
gotErr5 := errors.Unwrap(gotErr3) | |
fmt.Println(gotErr5) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment