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: unclear instruction for errors exercise #273

Open
adderek opened this issue Jul 28, 2017 · 3 comments · May be fixed by golang/website#218
Open

tour: unclear instruction for errors exercise #273

adderek opened this issue Jul 28, 2017 · 3 comments · May be fixed by golang/website#218

Comments

@adderek
Copy link

adderek commented Jul 28, 2017

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

The instruction does not clearly define what to do.
I was expecting an output:

1.41
cannot Sqrt negative number: -2

while the only output I was able to produce is

1.41 <nil>
0 cannot Sqrt negative number: -2

as fmt.Println(Sqrt(2)) will print "error"
and fmt.Println(Sqrt(-2)) will print the float64 part

It should be also noted earlier that %g is the way to print float64

My code:

type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
	return fmt.Sprintf("cannot Sqrt negative number: %g", float64(e))
}
func (e ErrNegativeSqrt) String() string {
	return fmt.Sprintf("%f",e)
}
func Sqrt(x float64) /*ErrNegativeSqrt*/(float64, error) {
	if x<0 {
		return 0,ErrNegativeSqrt(x) // Why do I need to include 0? nil seems more appropriate
	}
	var z1, z2 float64 = 1,0
	for i:=0; i<8; i++ {
		z2 = z1-((z1*z1-x)/(2*z1))
		z1=z2
	}
	return z2, nil
}
@mvdan
Copy link
Member

mvdan commented Jul 28, 2017

I was expecting an output:
1.41
cannot Sqrt negative number: -2

Why? If you're printing the result of the call, you'll get two elements.

@adderek
Copy link
Author

adderek commented Jul 28, 2017

Why? If you're printing the result of the call, you'll get two elements.

Because expected output was not clearly defined and sqrt(2) feels just right to return a single (might be complex) value. Remember that the tour is meant for newbies.
Please, just define bounding conditions: what is the input, what is expected output.
Also look at issues #230 and #59

@paulgrove
Copy link

👍 was unclear to me also what the exercise was intending.

Got there in the end, had to search internet (found this) when trying to return nil, ErrNegativeSqrt(x)

More focus on the standard return pattern for errors in the previous step with a full working implementation would have been appreciated.

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
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants