Skip to content

Instantly share code, notes, and snippets.

@smyrman
Last active September 8, 2019 23:01
Show Gist options
  • Save smyrman/6856be04c6ba636a7d285857753ae7ad to your computer and use it in GitHub Desktop.
Save smyrman/6856be04c6ba636a7d285857753ae7ad to your computer and use it in GitHub Desktop.
Go 1.13 const error example
pacakge x
const (
ErrPermissoinDenied = constErr("permissoin denied")
ErrInvalidParameters = constErr("invalid parameters")
)
type constErr string
func (err constErr) Is(target error) bool {
s := target.Error()
if s == string(err) || strings.HasPrefix(s, string(err)+": ")
}
func (err constErr) wrap(err error) error {
return wrapErr{msg: string(err), err: err}
}
type wrapErr struct {
msg string
err error
}
func (err wrapErr) Is(target error) bool {
return constErr(err.msg).Is(target)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment