Slices and Backing Arrays
CONCEPTS:Go Slices and Backing Arrays
Question Variations
- "Why can modifying one slice change another slice?"
- "When does `append` allocate a new backing array?"
- "What is the difference between a nil slice and an empty slice?"
- "How do you make an independent copy of a slice?"
Why This Is Asked
Slices are used throughout Go, yet their shared storage creates surprising mutation and memory-retention bugs. This question tests whether you can reason about length, capacity, append, and copying rather than treating a slice as an independent dynamic array.
Key Concepts
- A slice has a pointer, length, and capacity; it is not the array itself.
- Slicing and assigning slices can share the same backing array.
appendreuses capacity when possible and may allocate a new array when it is exhausted.copycopies elements into a destination slice and is useful for ownership boundaries.
Question Variations
- “Why can modifying one slice change another slice?”
- “When does
appendallocate a new backing array?” - “What is the difference between a nil slice and an empty slice?”
- “How do you make an independent copy of a slice?”