The error type is an interface type. An error variable represents any value that can describe itself as a string.
type error interface {
Error() string
}
Error handling in Go
in Go. You can choose either to or return the error as a value.
func (s *Eater) eat(food *Food) error {
if food == nil {
return errors.New("not given food")
}
food.Status = "eaten"
return nil // zero value of error is nil
}
func (s *Eater) eatOrPanic(food *Food) {
if food == nil {
panic("not given food")
}
food.Status = "eaten"
}
Custom Error Wrapper
Here's an example(excuse me for coming up with a bad one).