Arrays have fixed length: [3]int. Slices are dynamic views over arrays: []int. Slices are used far more often—similar to Python lists but backed by contiguous arrays.
Slice internals
A slice is a header (pointer, length, capacity). append may reallocate backing arrays—copy if sharing underlying arrays matters. Slices are reference types; assigning aliases the same backing array.
Common operations
s := []int{1, 2, 3}
s = append(s, 4)
sub := s[1:3] // half-open range
Important interview questions and answers
- Q: Array vs slice?
A: Arrays fixed size, value type; slices dynamic length, reference to backing array. - Q: append side effects?
A: May reallocate—other slices sharing old array won't see new elements if capacity exceeded.
Self-check
- What does s[1:3] include?
- How do you add an element to a slice?
Tip: Slices share backing arrays—copy with append([]T(nil), s...) or slices.Clone when mutation must not alias.
Interview prep
- Array vs slice?
Arrays fixed size and value type; slices dynamic views with pointer/len/cap header.
- append reallocation?
May allocate new backing array—other subslices may not see new elements if capacity exceeded.