Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tour: Exercise Errors, missing explanation #230

Open
pr0gramista opened this issue Jun 3, 2017 · 1 comment · May be fixed by golang/website#218
Open

tour: Exercise Errors, missing explanation #230

pr0gramista opened this issue Jun 3, 2017 · 1 comment · May be fixed by golang/website#218
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.

Comments

@pr0gramista
Copy link

Context: https://tour.golang.org/methods/20

Note: a call to fmt.Sprint(e) inside the Error method will send the program into an infinite loop. You can avoid this by converting e first: fmt.Sprint(float64(e)). Why?

There is no explanation even though it is introduced.

@ALTree ALTree added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Jun 29, 2018
@jjkavalam
Copy link

In fact, the statement does not seem to be true either.

https://go.dev/play/p/M7OT62bK-J7

package main

import (
	"fmt"
)

type ErrNegativeSqrt float64

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

func Sqrt(x float64) (float64, error) {
	if x < 0 {
		return 0.0, ErrNegativeSqrt(x)
	}
	return 0, nil
}

func main() {
	fmt.Println(Sqrt(2))
	fmt.Println(Sqrt(-2))
}

The code above executes to completion:

0 <nil>
0 cannot Sqrt negative number: -2.000000

Program exited.

I believe, fmt.Sprint will use the String() method in preference to Error() method ?

crisman added a commit to crisman/golang_website that referenced this issue Mar 20, 2023
In "tour/methods/20" ("Exercise: Errors") just after the slide
explaining "calling code should handle errors by testing whether the
error equals nil" the example code in the errors exercise provides a
main that calls functions expecting error values and does not check
them. Checking error values is the expected go way of doing things and
should be shown. In the successful case it is also odd to show the "nil"
of no error when printing the correct answer (see golang/tour#273).
Printing the likely undefined float64 root value when there is an
error is also confusing.

Update "methods/exercise-errors.go" and matched solution to only print
error on error and only root otherwise.

Fixes golang/tour#273
Fixes golang/tour#230
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants