0
0
Goprogramming~10 mins

Appending to slices in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Appending to slices
Start with slice
Call append(slice, value)
Check capacity
Add value
Return new slice with value
End
Start with a slice, append a value, check if capacity allows adding directly or needs new array, then return updated slice.
Execution Sample
Go
package main
import "fmt"
func main() {
  s := []int{1, 2}
  s = append(s, 3)
  fmt.Println(s)
}
This code starts with a slice of two integers, appends a third integer, and prints the updated slice.
Execution Table
StepActionSlice BeforeValue AppendedCapacity CheckSlice AfterOutput
1Initialize slice[]int{1, 2}-Capacity 2, length 2[]int{1, 2}-
2Append 3[]int{1, 2}3Capacity full, allocate new array[]int{1, 2, 3}-
3Print slice[]int{1, 2, 3}---[1 2 3]
💡 Program ends after printing the updated slice with appended value.
Variable Tracker
VariableStartAfter AppendFinal
s[]int{1, 2}[]int{1, 2, 3}[]int{1, 2, 3}
Key Moments - 2 Insights
Why does the slice get a new underlying array when appending?
Because the original slice's capacity is full (length equals capacity), Go allocates a new array with more space to hold the appended value, as shown in step 2 of the execution_table.
Does append modify the original slice or return a new one?
Append returns a new slice which may point to a new underlying array. You must assign it back to the variable (s = append(...)) to keep the updated slice, as shown in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what happens when appending value 3?
AThe value 3 is added directly because there is enough capacity.
BA new underlying array is allocated because capacity is full.
CThe slice remains unchanged.
DThe program crashes.
💡 Hint
Check the 'Capacity Check' and 'Slice After' columns in step 2 of the execution_table.
According to variable_tracker, what is the value of slice 's' after append?
A[]int{1, 2}
B[]int{3}
C[]int{1, 2, 3}
D[]int{}
💡 Hint
Look at the 'After Append' and 'Final' columns for variable 's' in variable_tracker.
If the original slice had capacity 4, what would change in the execution_table at step 2?
ACapacity check would say enough capacity, no new array allocated.
BCapacity check would still allocate new array.
CSlice would be empty after append.
DAppend would fail.
💡 Hint
Think about how capacity affects whether a new array is allocated during append, as shown in step 2.
Concept Snapshot
Appending to slices in Go:
- Use append(slice, value) to add elements.
- If capacity allows, value is added directly.
- If capacity is full, Go allocates a new underlying array.
- Always assign the result back to the slice variable.
- Example: s = append(s, 3)
Full Transcript
This visual execution shows how appending to slices in Go works step-by-step. We start with a slice of two integers. When we append a third integer, Go checks if the slice has enough capacity. Since the capacity is full, it allocates a new underlying array with more space and copies the old elements plus the new one. The append function returns this new slice, which we assign back to the variable. Finally, printing the slice shows the updated contents. Key points include understanding capacity, the need to assign the result of append, and how Go manages memory behind the scenes.