指针的声明
func main() {
a := 10
var p1 *int
p1 = &a
//指针保存的地址
fmt.Println(p1)
//指针本身的地址
fmt.Println(&p1)
//指针指向保存的数据
fmt.Println(*p1)
//通过指针,修改a的变量
*p1 = 100
fmt.Println(a)
var p2 **int
p2 = &p1
//p1的地址
fmt.Println(p2)
//p2的地址
fmt.Println(&p2)
fmt.Println(**p2)
}
数组指针
(*p3)[0] = 100
fmt.Println(arr)
p3[0] = 200
fmt.Println(arr)
fmt.Println("===========================")
b := 2
c := 3
d := 4
arr2 := [3]*int{&b, &c, &d}
b = 888
for i := 0; i < len(arr2); i++ {
fmt.Println(*arr2[i])
}
结构体
var person1 Person
//初始化默认所有字段的0值
fmt.Println(person1)
person2 := Person{}
person2.age = 20
person2.name = "Wayne"
//注意结尾有逗号
person3 := Person{
name: "wayne",
age: 20,
}
fmt.Println(person3)
person4 := Person{name: "Wayne"}
fmt.Println(person4)
//注意需要填写所有字段
person5 := Person{"Wayne", 20}
fmt.Println(person5)
person1 = person2
person1.age = 100
fmt.Println(person2)
type Person struct {
name string
age int
}
注意结构体是值类型
结构体字段提升
注意,需要是匿名字段
func main() {
student := Student{}
student.school = "BD"
student.name = "wang er gou"
student.age = 16
fmt.Println(student)
}
type Person struct {
name string
age int
}
type Student struct {
Person
school string
}
标签:p2,p1,Person,fmt,09,Println,Go,指针
From: https://www.cnblogs.com/huacha/p/16867961.html