我想我明白指针是什么,但我不太明白何时使用它.
以下片段来自"A Tour of Go".
"*Vertex"和"&Vertex"的目的是什么?
我用"顶点"替换它们,它运行正常.
package main
import (
"fmt"
"math"
)
type Vertex struct {
X, Y float64
}
func (v *Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
func main() {
v := &Vertex{3, 4}
fmt.Println(v.Abs())
}
Run Code Online (Sandbox Code Playgroud)