- 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)
控制台输出:
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]
//append方法会新建一个切片,切片与原切片公用一个底层数组
- 通过区间获取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:10, slice length:5, slice:[0 0 0 4 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:10, slice length:5, slice:[0 10 0 4 0]
newslice cap:10, newslice length:6, newSlice:[0 10 0 4 0 2]
otherslice cap:9, otherslice length:2, otherslice:[10 0]
otherslice2 cap:9, otherslice2 length:3, otherslice2:[10 0 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]