Returning an error
suggest changeTo indicate a failure, return a value of error type.
To indicate no error, return nil.
// somewhat inefficient way of appending a string at the end of a file
func appendToFile(path string, s string) error {
d, err := os.ReadFile(path)
if err != nil {
return err
}
d = append(d, []byte(s)...)
return os.WriteFile(path, d, 0644)
}
If the function also returns one or more values, error should be the last returned value:
func sqrt(n int) (int, error) {
if n < 0 {
return 0, fmt.Errorf("n should be >= 0, is %d", n)
}
return n*n, nil
}
Pitfalls
If you're using a custom error type, don't return that concrete type. Return the error interface:
type CustomError struct {
}
func (e *CustomError) Error() string {
return "this is a custom error"
}
func returnsCustomError(pleaseFail bool) *CustomError {
if pleaseFail {
return &CustomError{}
}
return nil
}
func caller() {
var err error
if err == nil {
fmt.Printf("err is nil, as expected\n");
}
err = returnsCustomError(false)
if err != nil {
fmt.Printf("returnsCustomError() failed with '%s'\n", err)
}
}
Unexpectedly it looks like returnsCustomError() returned an error even though it returned nil.
This is a variant of nil interface pitfall.
#note link to that chapter
Uninitialized err value is nil because it's the zero value of interface types.
returnsCustomError() returns nil value of CustomError struct.
This nil value is assigned to err variable. This is equivalent to:
var err error
err = (*CustomError)(nil)
At this point err is no longer the zero value of an interface. Its dynamic type is *CustomError and its dynamic value is nil.
Therefore err no longer equals nil.
To put it differently:
var err1 error
var err2 error = nil
var err3 error = (*CustomError)(nil)
if err1 == nil && err2 == nil {
fmt.Printf("uninitialized interface value and interface value assigned nil are both zero values of interface type and therefore equal to 'nil'\n")
}
if err3 != nil {
fmt.Printf("an interface holding a nil *CustomError is not the zero interface value and therefore is not equal to nil\n")
}