go append的坑

b := []int{1,2,3,4,5}


	slice := b[:2]

	newSlice := append(slice, 50)
	fmt.Println(b)
	fmt.Println(newSlice)

  

打印: 

[1 2 50 4 5]
[1 2 50]

append 底层源码实现如果发现切片指向原数组的容量足够大就不会扩容另外分配一个数组,而是修改原数组。  

posted on 2021-01-06 17:56  biwentao  阅读(168)  评论(0)    收藏  举报

导航