今天在旅途中工作。我注意到我可以將結構文字傳遞給與指向結構的指針相關的方法,反之亦然。爲什麼這是允許的?方法接收器含糊不清
package main
import (
"fmt"
)
type Vertex struct {
X, Y float64
}
func (v Vertex) Scale (f float64) {
v.X = v.X * f
v.Y = v.Y * f
}
func (v *Vertex) ScaleP(f float64) {
v.X = v.X * f
v.Y = v.Y * f
}
func main() {
v := &Vertex{3, 4}
vLiteral := Vertex{3, 4}
v.Scale(5)
fmt.Println(v)
v.ScaleP(5)
fmt.Println(v)
vLiteral.Scale(5)
fmt.Println(vLiteral)
vLiteral.ScaleP(5)
fmt.Println(vLiteral)
}
輸出:
&{3 4}
&{15 20}
{3 4}
{15 20}
參見[此](http://stackoverflow.com/q/13326099/ 720999)和[this](http://stackoverflow.com/q/13303254/720999)。 – kostix 2013-02-18 06:48:38