3

I'm working through the Go tour and in the Errors exercise it mentions that calling Sprint(f) in your Error function will result in an issue, which is an infinite loop. Why this occurs is explained here: Error, infinite loop

In my first implementation though I used Sprintf with the %f verb:

func (e ErrNegativeSqrt) Error() string {
    return fmt.Sprintf("cannot Sqrt negative number: %f", e)
}

This seems to avoid the issue and I was wondering if this is because the %f verb is expecting a float so it forces it to treat e as a float? The tour mentioned that assignment requires explicit conversion, however I assume that doesn't effect this case?

Or am I completely off the mark and something else is going on here?

4

1 Answer 1

4

That infinite loop case applies only to v, s, x, X, and q.

See here: https://github.com/golang/go/blob/6f51082da77a1d4cafd5b7af0db69293943f4066/src/fmt/print.go#L615

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.