New to Go, trying to solve Tour of Go, Exercise 56, which is about error handling.
Can the following error handling method can be further improved?
package main
import (
"fmt"
"math"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %f", float64(e))
}
func Sqrt(f float64) (float64, error) {
if f > 0 {
return math.Sqrt(f), nil
} else {
return 0, ErrNegativeSqrt(f)
}
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(Sqrt(-2))
}
return 0, ErrNegativeSqrt(f)
in an else block. Thego vet
tool would complain about this. \$\endgroup\$