声明:
源slice= src
添加slice = app
结果slice=tar
append时
len tar === len src + len app
1)如果len(src) + len(app) <= cap(src) cap tar = cap(src)
2)否则
a) len(src) + len(app) > 2* cap(src) cap tar = len(src) + len(app)
b) cap(src) < len(src) + len(app) <= 2* cap(src) cap tar = 2* cap(src)
data := make([]int, 10, 20) data[0] = 1 data[1] = 2 dataappend := make([]int, 12, 30)//修改这个len dataappend[0] = 1 dataappend[1] = 2 result := append(data, dataappend) result[0] = 99 result[11] = 98 fmt.Println("length:", len(data), "cap:", cap(data), ":", data) fmt.Println("result length:", len(result), "cap:", cap(result), ":", result) fmt.Println("length:", len(dataappend), "cap:", cap(dataappend), ":", dataappend)
有疑问加站长微信联系(非本文作者)