#### 内存指针
~~~
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中的引用