the-little-go-book_ZH_CN
the-little-go-book_ZH_CN copied to clipboard
关于切片cap的扩容
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