我谈到了方法中的指针,有些东西我不明白为什么。 我是这样写的:
type Vertex struct {
X,Y float64
}
func Abs(v Vertex) float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
func Scale(v *Vertex,f float64) {
v.X = v.X * f
v.Y = v.Y * f
}
func main() {
v := Vertex{3,4}
Scale(&v,10)
fmt.Println(Abs(v))
}
这是一个指针,并给出结果:50 但是,如果我要删除指针,那么代码如下所示:
type Vertex struct {
X,Y float64
}
func Abs(v Vertex) float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
func Scale(v Vertex,4}
Scale(v,10)
fmt.Println(Abs(v))
}
它给我的结果是:5
有人可以帮助我对此有所启发吗?