package main import ( "fmt" ) func main() { t1 := []int32{1, 2, 3} fmt.Println(printSelf(t1)) fmt.Println(t1) fmt.Println("................................") t2 := &[]int32{1, 2, 3} fmt.Println(&t2) t3 := printAddr(t2) fmt.Println(&t2, *t2) fmt.Println(&t3) fmt.Println("--------------------------------") t4 := []int32{1, 2, 3} fmt.Println(t4) printAddr2(t4) fmt.Println(t4) } func printSelf(arr []int32) []int32 { arr[0] = 0 return arr } func printAddr(addr *[]int32) *[]int32 { (*addr)[0] = 10 return addr } func printAddr2(addr []int32) []int32 { addr[0] = 10 return addr }
运行结果:
[ `go run test2.go` | done: 944.2326ms ]
[0 2 3]
[0 2 3]
................................
0xc082024028
0xc082024028 [10 2 3]
0xc082024030
--------------------------------
[1 2 3]
[10 2 3]
第34行不加括号会报错的!!!!
有疑问加站长微信联系(非本文作者)