#### 內存指針
~~~
type Vertex struct {
X int
Y int
}
func main() {
fmt.Println(Vertex{1, 2})
}
~~~
一個結構體(`struct`)就是一個字段的集合。
type 的含義跟其字面意思相符
#### 結構體指針
~~~
type Vertex struct {
X int
Y int
}
func main() {
v := Vertex{1, 2}
p := &v //把結構體的<引用>賦給變量p
//fmt.Print(p)//打印出的值為 &{1 2}
p.X = 33 //通過引用改變結構體里的X值
fmt.Println(v)
}
~~~
~~~
type Vertex struct {
X, Y int
}
var (
v1 = Vertex{1, 2} // {1 2} 類型為 Vertex
v2 = Vertex{X: 1} // {1 0} Y:0 被省略
v3 = Vertex{} // {0 0} X:0 和 Y:0
p = &Vertex{1, 2} // &{75 2} 類型為 *Vertex
)
func main() {
p.X = 75
fmt.Println(v1, v2, v3, p)
}
~~~
與內存指針不同的是,結構體指針更像是PHP中的引用