go slice扩容

go语言数组内存是怎么扩容的

话不多说,上测试代码和结果 package main import ( "fmt" ) func main() { var orderIds []int fmt.Println(len(orderIds), cap(orderIds)) fmt.Println("已使用", "\t\t", "容量", "\t\t", "增量") var lastCap = cap(orderIds) for i := 0; i < 100000; i++ { orderIds = append(orderIds, 1) if cap(orderIds) != lastCap{ fmt.Println(len(orderIds), "\t\t", cap(orderIds), "\t\t", cap(orderIds)-lastCap) lastCap = cap(orderIds) } } } 输出 0 0 已使用 容量 增量 1 2 2 3 4 2 5 8 4 9 16 8 ...
执行时间: 1713991126046.8 毫秒