0
0
Goprogramming~10 mins

Slice length and capacity in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Slice length and capacity
Create array
Create slice from array
Slice has length and capacity
Length = number of elements in slice
Capacity = elements from start to array end
Use len() and cap() to get values
Slice can grow up to capacity
Appending beyond capacity creates new array
This flow shows how a slice is created from an array, how its length and capacity are set, and how they affect slice behavior.
Execution Sample
Go
arr := [5]int{10, 20, 30, 40, 50}
slice := arr[1:3]
lenSlice := len(slice)
capSlice := cap(slice)
fmt.Println(lenSlice, capSlice)
Create an array and a slice from it, then print the slice's length and capacity.
Execution Table
StepActionSlice ContentLength (len(slice))Capacity (cap(slice))Explanation
1Create array arr = [10,20,30,40,50]---Array with 5 elements created
2Create slice = arr[1:3][20 30]24Slice from index 1 to 3 (exclusive), length=2, capacity=4 (from index 1 to end)
3Calculate len(slice)N/A2N/ALength is number of elements in slice
4Calculate cap(slice)N/AN/A4Capacity is number of elements from slice start to array end
5Print len and capN/A24Outputs: 2 4
6Append element 60 to slice[20 30 60]34Length increases, capacity unchanged as within capacity
7Append element 70 to slice[20 30 60 70]44Length equals capacity now
8Append element 80 to slice[20 30 60 70 80]58Capacity grows, new underlying array created
9Print final slice, len, cap[20 30 60 70 80]58Outputs: [20 30 60 70 80] 5 8
💡 Appending beyond capacity creates a new array with larger capacity, stopping the old capacity limit.
Variable Tracker
VariableStartAfter Step 2After Step 6After Step 7After Step 8Final
arr[10 20 30 40 50][10 20 30 40 50][10 20 30 60 50][10 20 30 60 70][10 20 30 60 70][10 20 30 60 70]
slicenil[20 30][20 30 60][20 30 60 70][20 30 60 70 80][20 30 60 70 80]
len(slice)023455
cap(slice)044488
Key Moments - 3 Insights
Why does the capacity of the slice start at 4 when the slice length is 2?
Because capacity counts elements from the slice start index to the end of the underlying array, not just the current length. See step 2 in execution_table.
What happens when we append an element beyond the slice's capacity?
A new underlying array is created with larger capacity, and the slice points to this new array. This is shown in step 8 where capacity grows from 4 to 8.
Does appending elements always increase capacity?
No, capacity only increases when appending exceeds current capacity. Steps 6 and 7 show appending within capacity, so capacity stays the same.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What is the capacity of the slice?
A4
B3
C2
D5
💡 Hint
Check the 'Capacity (cap(slice))' column at step 2 in execution_table.
At which step does the slice capacity increase beyond 4?
AStep 7
BStep 8
CStep 6
DStep 9
💡 Hint
Look at the capacity values in execution_table rows for steps 6 to 9.
If we append only one element to the initial slice, what will be the length and capacity?
ALength 3, Capacity 3
BLength 2, Capacity 4
CLength 3, Capacity 4
DLength 4, Capacity 4
💡 Hint
Refer to step 6 in execution_table for appending one element.
Concept Snapshot
Slice length and capacity in Go:
- length = number of elements in slice (len(slice))
- capacity = elements from slice start to array end (cap(slice))
- capacity >= length always
- appending beyond capacity creates new array
- use len() and cap() to check values
Full Transcript
This visual trace shows how a slice in Go is created from an array and how its length and capacity are determined. The slice length is the number of elements it currently holds, while capacity is how many elements it can hold starting from its first element to the end of the underlying array. When appending elements, if the length is less than capacity, the slice grows within the same array. If appending exceeds capacity, Go creates a new larger array and copies elements over. This behavior is shown step-by-step with variable values and slice contents.