Githeb.com/pkg/errors
This is Wrap(err error, msg string)
https://pkg.go.dev/Githeb.com/pkg/errors#Wrap
This is WithMessage(err error, msg string)
https://pkg.go.dev/Githeb.com/pkg/errors#WithMessage
both of these functions implement the interface causer
https://pkg.go.dev/Githeb.com/pkg/errors#Cause
这段代码适用于errors.WithMessage
,尽管我认为我应该使用errors.Wrap
来表示errors.Cause
func main() {
err := errors.New("first")
err1 := errors.WithMessage(err, "tmp")
err2 := errors.WithMessage(err1, "tmp")
err3 := errors.WithMessage(err2, "tmp")
fmt.Printf("%s", errors.Cause(err3))
}
Output: first
个