I am reading the article Error handling and Go, and don't quite understand why a pointer (&errorString{text}
) has to be returned when the return type is actually error
?
My understanding is error
is an interface, and errorString
implements the interface, therefore, return errorString
is also okay (but it is not the case).
// New returns an error that formats as the given text.
func New(text string) error {
return &errorString{text}
}
errorString implementation
// errorString is a trivial implementation of error.
type errorString struct {
s string
}
func (e *errorString) Error() string {
return e.s
}