Skip to content

Instantly share code, notes, and snippets.

@podhmo
Last active Sep 30, 2021
Embed
What would you like to do?
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