The guru package allows adding a Guru Meditation Number to errors:
const (
CodeFruitOverflow = iota + 1
CodeBoozeUnderrun
CodeExpired
)
func Example() {
err := guru.New(CodeFruitOverflow, "too many bananas")
fmt.Println(err)
code := guru.Code(err)
fmt.Println(code)
err = errors.New("not enough beer")
err = guru.WithCode(CodeBoozeUnderrun, err)
fmt.Println(err)
err = errors.New("Dennis Ritchie")
err = guru.Wrap(CodeExpired, err, "no longer with us")
fmt.Println(err)
err = guru.New(http.StatusNotAcceptable, "Justin Bieber")
fmt.Println(err)
err = guru.New(1, "oh noes")
err = guru.WithCode(2, err)
fmt.Println(guru.Code(err))
}
It's convenient for e.g. HTTP status codes, but also other things.
API docs: https://godocs.io/zgo.at/guru