當(dāng)append操作一個切片的時候待锈,如果操作之后的切片沒有超過原始切片的容量(cap)值時篡帕,新產(chǎn)生的切片與操作的切片公用同一個底層數(shù)組耗拓,如果超過了容量(cap)留美,則會新產(chǎn)生底層數(shù)組的新切片
- append新建newSlice查看 newSlice和slice之間的區(qū)別
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就不再公用一個底層數(shù)組,因為newslice cap超出了slice的cap
- 通過區(qū)間獲取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第一個位置開始獲取的數(shù)據(jù)景图,所以otherslice的第三個位置相當(dāng)于newslice的第四個位置
針對1和2兩種append方法造成的不同 個人理解 當(dāng)增加的數(shù)據(jù)索引在原始切片長度內(nèi)较雕,則會對原始切片的對應(yīng)位置造成影響,如果增加的數(shù)據(jù)的索引超過了原始切片長度挚币,則不會對原始切片造成影響
- 修改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是單獨的底層數(shù)組
newslice亮蒋,otherslice,otherslice2是公用同一個底層數(shù)組
- 函數(shù)傳值
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]