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: confusion with pointer and methods. #12962

Open
jcderner opened this issue Oct 16, 2015 · 3 comments

Comments

@jcderner
Copy link

commented Oct 16, 2015

Context: http://tour.golang.org/methods/3

I played around with the code and tried this:
.....
func (v Vertex) Scale(f float64) Vertex {
v.X = v.X * f
v.Y = v.Y * f
return v
}
...
func main() {
v := &Vertex{3, 4}
fmt.Printf("Before scaling: %+v, Abs: %v\n", v, v.Abs())
v = &v.Scale(5)
fmt.Printf("After scaling: %+v, Abs: %v\n", v, v.Abs())
}

The compiler gives the error: prog.go:25: cannot take the address of v.Scale(5)
But if I write instead:
w := v.Scale(5)
v = &w

..., then the code works.
This was confusing for me! Can you clarify this?

@ianlancetaylor ianlancetaylor added this to the Unreleased milestone Oct 16, 2015

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Oct 16, 2015

@rakyll

This comment has been minimized.

Copy link
Member

commented Oct 19, 2015

How we can give advice about syntax rules without hinting the solution? It must be user's responsibility to figure out what the compilation error means and how to fix it.

@jcderner

This comment has been minimized.

Copy link
Author

commented Oct 20, 2015

Rakyll,

thank you for the feedback.
Ok, clear now - after some thinking and checking the spec.; the return value from the function call is not addressable.
But maybe it is worth to mention it in the tutorial/tour?
Maybe others - that don't come from e.g. a C tradition - hit the same problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.