Skip to content

Instantly share code, notes, and snippets.

@jonmorehouse
Created July 13, 2015 06:02
Show Gist options
  • Save jonmorehouse/96e6eae1d90ff83ae663 to your computer and use it in GitHub Desktop.
Save jonmorehouse/96e6eae1d90ff83ae663 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"time"
)
type ErrorA struct {}
func (e *ErrorA) Error() string { return "error a" }
type ErrorB struct {}
func (e ErrorB) Error() string { return "error b" }
func GetError() error {
now := time.Now().UnixNano()
r := rand.New(rand.NewSource(now))
if r.Int() % 2 == 0 {
return &ErrorA{}
} else {
return ErrorB{}
}
}
func ErrorSwitch() {
err := GetError();
switch err.(type) {
case nil:
fmt.Println("No error")
case ErrorB:
fmt.Println("error type b")
case *ErrorA:
fmt.Println("error type a")
case error:
fmt.Println("generic error")
}
}
func main() {
ErrorSwitch()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment