3

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.

1 Answer 1

9

The documentation states how the value is converted to a string:

  1. If an operand implements the error interface, the Error method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).

  2. If an operand implements method String() string, that method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).

The error interface comes before Stringer.

A more idiomatic way to write the function is:

func Sqrt(x float64) (float64, error) {
  if x < 0 {
    return 0, ErrNegativeSqrt(x)
  }
  return math.Sqrt(x), nil
}
3
  • Any other way to make it print the regular string instead of the error message?
    – Srisa
    Apr 14, 2017 at 15:13
  • @Srisa: fmt.Println(Sqrt(2).String()) ? Apr 14, 2017 at 15:19
  • 3
    It's a two-argument return value, so that won't work. But frankly, that reflects a problematic design in general. Functions can return multiple values, so there's no real reason for your result item to also implement the error interface when you can just pass back an error itself (which you're doing, by literally passing the same item back twice). Bad code smell is the problem here.
    – Kaedys
    Apr 14, 2017 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.