the-little-go-book_ZH_CN icon indicating copy to clipboard operation
the-little-go-book_ZH_CN copied to clipboard

关于切片cap的扩容

Open cx4 opened this issue 5 years ago • 0 comments

func main() { scores := make([]int, 0, 5) c := cap(scores) fmt.Println(c) for i := 0; i < 25; i++ { scores = append(scores, i) // 如果容量已经改变,go为了容下这些新数据,不得不增长数组的长度 if cap(scores) != c { c = cap(scores) fmt.Println(c) } } } 看到文章写的是,扩容三次5、10、20 但是代码实际执行的时候是6、24、48

cx4 avatar May 20 '19 06:05 cx4