Slice length and capacity
Every slice has two numbers you should know: length and capacity.
- Length (
len(s)): how many elements the slice currently holds. - Capacity (
cap(s)): how many elements are available in the underlying array, starting from the slice’s first element.
You can grow a slice by re-slicing it — but only up to its capacity. Go beyond that and you’ll get a runtime panic.
Try it: Modify one of the slice operations in the example to exceed its capacity and observe the error.