Skip to content

Instantly share code, notes, and snippets.

@YanhaoYang
Last active May 11, 2017 05:47
Show Gist options
  • Save YanhaoYang/185bf0629228ea0d51b351614284a415 to your computer and use it in GitHub Desktop.
Save YanhaoYang/185bf0629228ea0d51b351614284a415 to your computer and use it in GitHub Desktop.
Handle named errors with switch-case
// BaseError ...
type BaseError struct {
s string
}
func (e *BaseError) Error() string {
return e.s
}
// SomeError ...
type SomeError struct {
BaseError
}
// OtherError ...
type OtherError struct {
BaseError
}
func main() {
err := &SomeError{BaseError{"some error"}}
switch err := err.(type) {
case *SomeError:
//...
case *OtherError:
//...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment