一个go切片的问题
var i = 3
nums := []int{1, 2, 3, 4, 5, 6}
nums6 := append(nums[:i], nums[i+2:]...)
fmt.Println(nums, nums6)
这段代码nums 最终输出为什么变成了[1,2,3,6,5,6]
输出内容为:`// [1 2 3 6 5 6] [1 2 3 6]`
看这个输入nums应该是改动了,但是为什么5,6又还在呢
看官方对 "append"
描述,"Appending_and_copying_slices" (https://link.segmentfault.com/?enc=D2Ws4zNWggDDQLPCoFLnQQ%3D%3D.nFDGR%2FkzXdRA8CEp7A5%2BBVg%2BPY98bO%2BPpYTu4C3sV1O0go1MDG%2FlLRSbwgPgbewv40WncmpfomZI7VjX1OJIgw%3D%3D)
首先 "append" 用法:
«append(s S, x ...E) S // core type of S is []E»
里面这么说的:
«If the capacity of "s" is not large enough to fit the additional values,
append allocates a new, sufficiently large underlying array that fits both
the existing slice elements and the additional values. Otherwise, append
re-uses the underlying array.»
如果 "append" 之后的数据并没有超过 "s" 原本的容量,那么就会利用原本的 "底层数组",也就是你等于是在原切片的底层数据上对应位置进行了改动。
然后对于切片的解释: "slices-intro" (https://link.segmentfault.com/?enc=KE%2FcRmEHFN1QJLgvHcokRQ%3D%3D.pyBECQraD%2FxzLgeJNy7Xh5GPa%2BS87l8udwZaCf9vUoYwD%2BzAQvmPsA%2FtrmqjujbB)
golang中切片由三部分组成:
"image.png" (https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241006/343b2701b716f89a80cd3f00439115df.png)
所以结合 "append" 不超过原本切片对象的容量时候,在原本指针所指向的 "underlying array"
做了修改,并返回;如果超过原本切片的容量,那就会重新 "reallocate" 一个新的 "underlying array"予以返回。
将你的例子进行扩展,不改变容量的时候:
nums8 := append(nums[:4], 100)
fmt.Println("------", nums8, nums)
// ------ [1 2 3 4 100] [1 2 3 4 100 6]
就是这个道理。。。