Junior — Middle
What does the capacity property of a slice in Go mean if it is not explicitly specified during creation?
sobes.tech AI
Answer from AI
In Go, a slice is a structure that contains a pointer to an array, a length (len), and a capacity (cap).
If the capacity is not explicitly specified when creating a slice, it is set equal to the length of the slice. Capacity indicates how many elements the slice can hold without allocating new memory.
For example:
s := make([]int, 3) // len=3, cap=3
Here, the capacity is 3 because it is not specified separately.
If the capacity is greater than the length, the slice can be extended up to the capacity without allocating a new array.