I have this code
package main
import (
"fmt"
"math"
)
type ErrNegativeSqrt float64
func (s ErrNegativeSqrt) String() string {
return fmt.Sprintf("%f", float64(s))
}
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("Cannot Sqrt negative number: %v", float64(e))
}
func Sqrt(x float64) (ErrNegativeSqrt, error) {
if x < 0 {
e := ErrNegativeSqrt(x)
return e, e
} else {
return ErrNegativeSqrt(math.Sqrt(x)), nil
}
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(Sqrt(-2))
}
And the output of this code is
Cannot Sqrt negative number: 1.4142135623730951 <nil> Cannot Sqrt negative number: -2 Cannot Sqrt negative number: -2
When I have implemented the Stringer interface for the ErrNegativeSqrt, why is the fmt.Println invoking the Error() method instead of the String() method?
I am new to go, so I might be missing something very obvious.