大佬教程收集整理的这篇文章主要介绍了golang 数组与切片,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
类型
数组是值类型,将一个数组赋值给另一个数组时,传递的是一份拷贝。
切片是引用类型,切片包装的数组称为该切片的底层数组。
我们来看一段代码
当对slice append 超出底层数组的界限时
@H_404_10@//n1是n2的底层数组 n1 := [3]int{1,3} n2 := n1[0:3] fmt.Println("address of items in n1: ") for i := 0; i < len(n1); i++ { fmt.Printf("%p\n",&n1[i]) } //address of items in n1: //0xc20801e160 //0xc20801e168 //0xc20801e170 fmt.Println("address of items in n2: ") for i := 0; i < len(n2); i++ { fmt.Printf("%p\n",&n2[i]) } //address of items in n2: //0xc20801e160 //0xc20801e168 //0xc20801e170 //对n2执行append操作后,n2超出了底层数组n1的j n2 = append(n2,1) fmt.Println("address of items in n1: ") for i := 0; i < len(n1); i++ { fmt.Printf("%p\n",&n1[i]) } //address of items in n1: //0xc20801e160 //0xc20801e168 //0xc20801e170 fmt.Println("address of items in n2: ") for i := 0; i < len(n2); i++ { fmt.Printf("%p\n",&n2[i]) } //address of items in n2: //0xc20803a2d0 //0xc20803a2d8 //0xc20803a2e0 //0xc20803a2e8打印出来的结果如下:
@H_404_10@[main] the address of xyz is 0xc2080365f0 [rmlast] the address of a is 0xc2080365f0 [rmlast] after remove,the address of a is 0xc2080365f0 [main] after remove,the address of xyz is 0xc2080365f0 [1 2 3 4 5 6 7 8 9]这里直接打印了slice的指针值,因为Slice是引用类型,所以指针值都是相同的,我们换成打印slice的地址看下
@H_404_10@func rmLast(a []int) { fmt.Printf("[rmlast] the address of a is %p",&a) a = a[:len(a)-1] fmt.Printf("[rmlast] after remove,&a) } func main() { xyz := []int{1,&xyz) rmLast(xyz) fmt.Printf("[main] after remove,&xyz) fmt.Printf("%v",xyz) //[1 2 3 4 5 6 7 8 9] }结果:
@H_404_10@[main] the address of xyz is 0xc20801e1e0 [rmlast] the address of a is 0xc20801e200 [rmlast] after remove,the address of a is 0xc20801e200 [main] after remove,the address of xyz is 0xc20801e1e0 [1 2 3 4 5 6 7 8 9]这次可以看到slice作为函数参数传入函数时,实际上也是拷贝了一份slice,因为Slice本身是个指针,所以从现象来看,slice是引用类型
以上是大佬教程为你收集整理的golang 数组与切片全部内容,希望文章能够帮你解决golang 数组与切片所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。