Junior — Middle
How to determine the capacity of a slice in the Go programming language?
sobes.tech AI
Answer from AI
In Go, the capacity of a slice can be determined using the built-in cap() function. It returns the number of elements the slice can hold without reallocating memory.
Example:
s := make([]int, 3, 5) // length 3, capacity 5
fmt.Println(len(s)) // 3
fmt.Println(cap(s)) // 5
Here, len(s) is the current length of the slice, and cap(s) is its capacity.