Last active
September 30, 2021 05:52
-
-
Save podhmo/f5035fe427ac3bded749b14d5f26e4c7 to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"log" | |
"github.com/pkg/errors" | |
) | |
var ErrFoo = fmt.Errorf("FOO") | |
func main() { | |
if err := run(); err != nil { | |
log.Fatalf("!! %+v", err) | |
} | |
} | |
func run() error { | |
fmt.Println("foo is Foo?", errors.Is(foo(), ErrFoo)) // true | |
fmt.Println("withWrap-foo is Foo?", errors.Is(withWrap(), ErrFoo)) // true | |
fmt.Println("withWrap-foo cause Foo?", errors.Cause(withWrap()) == ErrFoo) // true | |
fmt.Println("errorf-foo is Foo?", errors.Is(withErrorf(), ErrFoo)) // true | |
fmt.Println("errorf-foo cause Foo?", errors.Cause(withErrorf()) == ErrFoo) // false | |
return nil | |
} | |
func foo() error { | |
return ErrFoo | |
} | |
func withWrap() error { | |
if err := foo(); err != nil { | |
return errors.Wrap(err, "with pkg.errors.Wrap") | |
} | |
return nil | |
} | |
func withErrorf() error { | |
if err := foo(); err != nil { | |
return fmt.Errorf("with errorf: %w", err) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment