func modifySliceData(data []int) {
data[0] = 0
data[1] = 0
data[2] = 0
fmt.Printf("data -- %p %T: &data=%p data=%v\n", data, data, &data, data)
}
func main() {
a := []int{1, 2, 3}
fmt.Printf("a -- %p %T: &a=%p a=%v\n", a, a, &a, a)
fmt.Println(a)
modifySliceData(a)
fmt.Println(a)
fmt.Printf("a =====%p %T: &a=%p a=%v\n", a, a, &a, a)
}
打印的结果:
a -- 0xc8200101a0 []int: &a=0xc820010180 a=[1 2 3]
[1 2 3]
data -- 0xc8200101a0 []int: &data=0xc820010260 data=[0 0 0]
[0 0 0]
a =====0xc8200101a0 []int: &a=0xc820010180 a=[0 0 0]
谁能解释下 为什么 0xc8200101a0 相同 但是 后面不同呢
有疑问加站长微信联系(非本文作者)