注:本文以Windos系统上Go SDK 1.18进行讲解
func main() {
var a = []int{1, 4}
fmt.Println(&a) //&[1 4]
fm5t.Printf("%p", a) //0xc0000160b0
}
func main() {
var a = []int{1, 4}
var p *[]int
p = &a
fmt.Println(*p) //输出:[1 4]
}
func main() {
a := []int{1, 2, 3}
fmt.Printf("%p\n", &a) //0xc000096060
}
func main() {
a := []int8{1, 2, 3}
fmt.Printf("%p\n", &a)
fmt.Println(&a[0])
fmt.Println(&a[1])
fmt.Println(&a[2])
b := []int16{1, 2, 3}
fmt.Printf("%p\n", &b)
fmt.Println(&b[0])
fmt.Println(&b[1])
fmt.Println(&b[2])
/*
0xc000004078
0xc000016098
0xc000016099
0xc00001609a
0xc000004090
0xc0000160d0
0xc0000160d2
0xc0000160d4
*/
}
func main() {
a := []int8{1, 2, 3}
fmt.Printf("%p\n", a)
fmt.Println(&a[0])
fmt.Println(&a[1])
fmt.Println(&a[2])
b := []int16{1, 2, 3}
fmt.Printf("%p\n", b)
fmt.Println(&b[0])
fmt.Println(&b[1])
fmt.Println(&b[2])
/*
0xc000102058
0xc000102058
0xc000102059
0xc00010205a
0xc000102090
0xc000102090
0xc000102092
0xc000102094
*/
}
func main() {
var a = []int{1, 4}
p := make([]*int, 3)
p[0] = &a[0]
fmt.Println(*p[0]) //输出:1
}
func main() {
var stack *[]int
fmt.Println(stack) //输出:
if stack == nil {
fmt.Println("yes") //输出:yse
} else {
fmt.Println("no")
}
}
func main() {
var a = []int{1, 4}
var p *[]int
p = &a
fmt.Println((*p)[1])//输出:4
}
但是数组可以使用
func main() {
var a = [3]int{1, 4}
var p *[3]int
p = &a
fmt.Println(p[1]) //输出:4
}
func main() {
var a = [3]int{1, 4}
var p *[3]int
p = &a
fmt.Println((*p)[1]) //输出:4
}