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
Open

tour: confusion with pointer and methods. #12962

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

Comments

@jcderner
Copy link

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
Copy link
Contributor

CC @adg @campoy

@rakyll
Copy link
Contributor

rakyll 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
Copy link
Author

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

No branches or pull requests

3 participants