Error constants in Golang
Error constants in Golang
Something that’s been bugging me for years is the fact that you can’t just declare errors as constants in Golang using just the standard library. I finally got annyoed enough by it to do some googling on Bing, and found this great solution by Jonathan Hall on boldlygo.tech from last year:
type errString string
func (e errString) Error() string {
return string(e)
}
const ErrTooManyFrogs = errString("too many frogs")
This is a great idea, and a great start, but I wanted to be able to just drop-in a package in place
of the standard library’s "errors" package, so came up with this: