如下所示函数,
func testArrayInt() (slice []int, p *[]int) { slice = make([]int, 100) p = &slice return slice, p }
在main函数中有这么一段代码,
sss, ppp := testArrayInt() fmt.Println(&sss) fmt.Println(ppp) sss[0] = 111; sss[1] = 222; fmt.Println(sss[0]) fmt.Println(sss[1]) fmt.Println((*ppp)[0]) fmt.Println((*ppp)[1])
打印
&[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] &[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 111 222 111 222
返回 slice 的指针和返回 slice 有什么区别呢 ?
还有一种情况:
如下函数,
func testInterface() (slice interface{}, p interface{}) { slice = make([]int, 100) p = &slice return slice, p }
在main函数中,
ss, pp := testInterface() fmt.Println(&ss) fmt.Println(pp)打印结果,
0xc82000a910 0xc82000a920
这两个值不应该相同吗 ?或者说他们底层是什么原理导致打印的两个值不同?
还请大神们指教!