skip to content
Alvin Lucillo

Go slice capacity

/ 1 min read

💻 Tech

Go’s slice capacity via cap is the length of the underlying array of a slice. When you append an element to a slice, its underlying array also grows. However, if the length exceeds the capacity, a new array is created with double the capacity of the old array.

val := []int{1,2,3}
fmt.Println(len(val), cap(val)) // 3 3

val = append(val, 4)
fmt.Println(len(val), cap(val)) // 4 6