Slice的坑

1numList := make([]int, 10) 
2// 产生的numList会存入10个0, 如果继续append 数据会导致numList的数据超过10

Slice 扩容

 1func TestSliceExtend(t *testing.T) {
 2	capacity := 0
 3	list := make([]int, 0)
 4	for i := 0; i < 4096; i++ {
 5		list = append(list, i)
 6		if capacity != cap(list) {
 7			times := float64(cap(list)) / float64(capacity)
 8			differ := cap(list) - capacity
 9			capacity = cap(list)
10			if times == 2.0 {
11				fmt.Printf("capacity is: %d \t times: %.2f \n", capacity, times)
12			} else {
13				fmt.Printf("capacity is: %d \t times: %.2f \t differ: %d \n", capacity, times, differ)
14			}
15		}
16	}
17}