tour: confusion with pointer and methods. #12962
Labels
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
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?
The text was updated successfully, but these errors were encountered: