当append操作一个切片的时候,如果操作之后的切片没有超过原始切片的容量(cap)值时,新产生的切片与操作的切片公用同一个底层数组,如果超过了容量(cap),则会新产生底层数组的新切片
- append新建newSlice查看 newSlice和slice之间的区别
slice := make([]int, 5)
fmt.Printf("slice cap:%d, slice length:%d, slice:%v\n",cap(slice), len(slice), slice)
//slice cap:5, slice length:5, slice:[0 0 0 0 0]
newSlice := append(slice, 2)
fmt.Printf("slice cap:%d, slice length:%d, slice:%v\n",cap(slice), len(slice), slice)
fmt.Printf("newslice cap:%d, newslice length:%d, newSlice:%v\n",cap(newSlice), len(newSlice), newSlice)
newSlice[1] = 1
fmt.Printf("slice cap:%d, slice length:%d, slice:%v\n",cap(slice), len(slice), slice)
fmt.Printf("newslice cap:%d, newslice length:%d, newSlice:%v\n",cap(newSlice), len(newSlice), newSlice)
控制台输出:
slice cap:5, slice length:5, slice:[0 0 0 0 0]
newslice cap:10, newslice length:6, newSlice:[0 0 0 0 0 2]
slice cap:5, slice length:5, slice:[0 0 0 0 0]
newslice cap:10, newslice length:6, newSlice:[0 1 0 0 0 2]
//append方法会新建一个切片,这时候的newSlice和slice就不再公用一个底层数组,因为newslice cap超出了slice的cap
- 通过区间获取newSlice位置1和2两个元素生成otherSlice,append方法增加一个元素4并生成新的切片otherSlice2
otherSlice := newSlice[1:3]
otherSlice2 := append(otherSlice, 4)
fmt.Printf("slice cap:%d, slice length:%d, slice:%v\n",cap(slice), len(slice), slice)
fmt.Printf("newslice cap:%d, newslice length:%d, newSlice:%v\n",cap(newSlice), len(newSlice), newSlice)
fmt.Printf("otherslice cap:%d, otherslice length:%d, otherslice:%v\n",cap(otherSlice), len(otherSlice), otherSlice)
fmt.Printf("otherslice2 cap:%d, otherslice2 length:%d, otherslice2:%v\n",cap(otherSlice2), len(otherSlice2), otherSlice2)
控制台输出:
slice cap:5, slice length:5, slice:[0 0 0 0 0]
newslice cap:10, newslice length:6, newSlice:[0 0 0 4 0 2]
otherslice cap:9, otherslice length:2, otherslice:[0 0]
otherslice2 cap:9, otherslice2 length:3, otherslice2:[0 0 4]
可以看到slice第四个位置不变,newslice第四个位置和otherslice2的第三个位置的值都变成了4,因为otherslice是从newslice第一个位置开始获取的数据,所以otherslice的第三个位置相当于newslice的第四个位置
针对1和2两种append方法造成的不同 个人理解 当增加的数据索引在原始切片长度内,则会对原始切片的对应位置造成影响,如果增加的数据的索引超过了原始切片长度,则不会对原始切片造成影响
- 修改otherSlice2的第一个元素为10
otherSlice2[0] = 10
fmt.Printf("slice cap:%d, slice length:%d, slice:%v\n",cap(slice), len(slice), slice)
fmt.Printf("newslice cap:%d, newslice length:%d, newSlice:%v\n",cap(newSlice), len(newSlice), newSlice)
fmt.Printf("otherslice cap:%d, otherslice length:%d, otherslice:%v\n",cap(otherSlice), len(otherSlice), otherSlice)
fmt.Printf("otherslice2 cap:%d, otherslice2 length:%d, otherslice2:%v\n",cap(otherSlice2), len(otherSlice2), otherSlice2)
控制台输出:
slice cap:5, slice length:5, slice:[0 0 0 0 0]
newslice cap:10, newslice length:6, newSlice:[0 10 0 0 0 2]
otherslice cap:9, otherslice length:5, otherslice:[10 0 0 0 2]
otherslice2 cap:9, otherslice2 length:6, otherslice2:[10 0 0 0 2 4]
这个例子更好的看出来slice是单独的底层数组
newslice,otherslice,otherslice2是公用同一个底层数组
- 函数传值
fmt.Printf("otherSlice2 Point:%p\n",&otherSlice2)
modifySlice(otherSlice2)
fmt.Printf("slice cap:%d, slice length:%d, slice:%v\n",cap(slice), len(slice), slice)
fmt.Printf("newslice cap:%d, newslice length:%d, newSlice:%v\n",cap(newSlice), len(newSlice), newSlice)
fmt.Printf("otherslice cap:%d, otherslice length:%d, otherslice:%v\n",cap(otherSlice), len(otherSlice), otherSlice)
fmt.Printf("otherslice2 cap:%d, otherslice2 length:%d, otherslice2:%v\n",cap(otherSlice2), len(otherSlice2), otherSlice2)
func modifySlice(slice []int) {
fmt.Printf("modify function Point:%p\n",&slice)
slice[1] = 20
slice = append(slice,50,60)
}
控制台输出:
otherSlice2 Point:0xc42000a300
modify function Point:0xc42000a420
slice cap:10, slice length:5, slice:[0 10 20 4 50]
newslice cap:10, newslice length:6, newSlice:[0 10 20 4 50 60]
otherslice cap:9, otherslice length:2, otherslice:[10 20]
otherslice2 cap:9, otherslice2 length:3, otherslice2:[10 20 4]