Skip to content

Instantly share code, notes, and snippets.

@podhmo
Last active September 30, 2021 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/f5035fe427ac3bded749b14d5f26e4c7 to your computer and use it in GitHub Desktop.
Save podhmo/f5035fe427ac3bded749b14d5f26e4c7 to your computer and use it in GitHub Desktop.
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