Bird
Raised Fist0
Goprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. In Go, what does the len() function return when used on a slice?
easy
A. The maximum number of elements the slice can hold
B. The total memory size of the slice in bytes
C. The index of the last element in the slice
D. The number of elements currently in the slice

Solution

  1. Step 1: Understand the purpose of len() on slices

    The len() function returns how many elements are currently stored in the slice.
  2. Step 2: Differentiate from capacity

    Capacity is about how many elements the slice can hold before resizing, not the current count.
  3. Final Answer:

    The number of elements currently in the slice -> Option D
  4. Quick Check:

    len(slice) = current element count [OK]
Hint: Remember: len = current items, cap = max before grow [OK]
Common Mistakes:
  • Confusing length with capacity
  • Thinking len returns memory size
  • Assuming len gives last index
2. Which of the following is the correct way to create a slice with length 3 and capacity 5 in Go?
easy
A. make([]int, 5, 3)
B. make([]int, 3, 5)
C. []int{3, 5}
D. make([]int, 3)

Solution

  1. Step 1: Recall the make function signature for slices

    make([]T, length, capacity) creates a slice with given length and capacity.
  2. Step 2: Match length and capacity values

    To get length 3 and capacity 5, use make([]int, 3, 5).
  3. Final Answer:

    make([]int, 3, 5) -> Option B
  4. Quick Check:

    make([]int, length, capacity) [OK]
Hint: make([]T, len, cap) sets length then capacity [OK]
Common Mistakes:
  • Swapping length and capacity arguments
  • Using slice literal instead of make
  • Omitting capacity when needed
3. What will be the output of this Go code?
nums := make([]int, 2, 4)
nums[0] = 10
nums[1] = 20
fmt.Println(len(nums), cap(nums))
medium
A. 2 2
B. 0 4
C. 2 4
D. 4 4

Solution

  1. Step 1: Analyze slice creation

    make([]int, 2, 4) creates a slice with length 2 and capacity 4.
  2. Step 2: Check length and capacity after assignments

    Assigning values to existing indices does not change length or capacity.
  3. Final Answer:

    2 4 -> Option C
  4. Quick Check:

    len = 2, cap = 4 [OK]
Hint: Length stays same unless you append beyond capacity [OK]
Common Mistakes:
  • Confusing length with capacity
  • Assuming capacity equals length after assignment
  • Thinking length changes when assigning values
4. Consider this code snippet:
arr := []int{1, 2, 3, 4, 5}
slice := arr[1:3]
fmt.Println(len(slice), cap(slice))

What is the correct output?
medium
A. 2 4
B. 2 2
C. 3 4
D. 3 5

Solution

  1. Step 1: Determine length of the slice

    The slice is from index 1 to 3 (exclusive), so length = 3 - 1 = 2.
  2. Step 2: Calculate capacity of the slice

    Capacity is from start index to end of original array: 5 - 1 = 4.
  3. Final Answer:

    2 4 -> Option A
  4. Quick Check:

    len = 2, cap = 4 from slice indices [OK]
Hint: Capacity = original array length - slice start index [OK]
Common Mistakes:
  • Using slice length as capacity
  • Confusing slice end index with capacity
  • Assuming capacity equals original array length
5. You have a slice s := make([]int, 3, 5). You append two elements to it. What will be the length and capacity of s after these appends?
hard
A. Length 5, Capacity 5
B. Length 5, Capacity 10
C. Length 3, Capacity 5
D. Length 5, Capacity 6

Solution

  1. Step 1: Understand initial slice size

    Slice s starts with length 3 and capacity 5.
  2. Step 2: Append two elements within capacity

    Appending two elements increases length to 5, capacity remains 5 because it is not exceeded.
  3. Final Answer:

    Length 5, Capacity 5 -> Option A
  4. Quick Check:

    Appending within capacity keeps capacity same [OK]
Hint: Capacity grows only when length exceeds capacity [OK]
Common Mistakes:
  • Assuming capacity doubles immediately after append
  • Confusing length with capacity after append
  • Thinking capacity increases by 1 per append
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard