You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

31 lines
457 B

package trading
import (
"fmt"
"testing"
)
func TestSliceCap(t *testing.T) {
Max := 1280
var arr []int
prevCap := cap(arr)
for i := range 1500 {
ele := i
if len(arr) < Max {
arr = append(arr, ele)
} else {
length := len(arr)
copy(arr, arr[length-Max+1:])
arr[Max-1] = ele
arr = arr[:Max]
}
c := cap(arr)
if c != prevCap {
// 发生扩容
prevCap = c
fmt.Printf("i=%d, cap=%d\n", i, c)
}
}
fmt.Println(arr)
}