You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
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?
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: