Introduction
A slice is Go's primary abstraction for working with sequences of data. Unlike an array, a slice does not have a fixed size baked into its type; instead, it describes a window into an underlying array using a pointer, a length, and a capacity. Slices can grow using the built-in append function, which may allocate a new, larger backing array when the current capacity is exceeded. This combination of flexibility and efficiency is why slices, not arrays, are used throughout everyday Go code.
Cricket analogy: A fixed-size array is like a stadium with a set number of seats, while a slice is like a scoreboard view into that stadium showing only certain rows; when more fans arrive than the view allows, append moves the view to a bigger stadium entirely.
Syntax
// Slice literal
nums := []int{1, 2, 3}
// make(type, length, capacity)
buf := make([]int, 0, 10)
// Slicing an existing array or slice: s[low:high]
arr := [5]int{10, 20, 30, 40, 50}
sub := arr[1:3] // [20 30]
// Appending
buf = append(buf, 1, 2, 3)Explanation
make([]int, 0, 10) creates a slice with length 0 and capacity 10, preallocating room to grow without reallocating on every append. The slicing expression s[low:high] selects elements from index low up to but not including high, and the result shares the same underlying array as the original, meaning mutations through one slice can be visible through the other. append adds elements to the end; if the slice's capacity is exhausted, Go allocates a new, larger array (commonly doubling capacity for smaller slices) and copies the existing elements over, after which the slice header points to the new array. This sharing behavior is a common source of bugs when two slices unexpectedly alias the same memory.
Cricket analogy: make([]int, 0, 10) is like reserving 10 seats in the stands before fans arrive, so no reshuffling is needed as people fill in; s[low:high] is like selecting overs 3 through 7 from the same scorecard, and append that exceeds capacity is like moving the whole crowd to a bigger stand and copying the seating chart.
Example
package main
import "fmt"
func main() {
original := []int{1, 2, 3, 4, 5}
window := original[1:3]
fmt.Println("window:", window, "len:", len(window), "cap:", cap(window))
// Mutating the window affects the original backing array
window[0] = 99
fmt.Println("original after mutation:", original)
// append may or may not reallocate depending on capacity
grown := append(window, 100)
fmt.Println("grown:", grown)
fmt.Println("original after append:", original)
}Output
window: [2 3] len: 2 cap: 4
original after mutation: [1 99 3 4 5]
grown: [99 3 100]
original after append: [1 99 3 100 5]Key Takeaways
- A slice header holds a pointer to an array, a length, and a capacity.
- Slicing shares the underlying array, so mutations can be visible across slices.
- append grows a slice, reallocating a new backing array once capacity is exceeded.
- make(type, len, cap) lets you preallocate capacity to reduce reallocations.
- Because slices can silently alias memory, copy() is used to make independent copies when needed.
Practice what you learned
1. What three pieces of information does a slice header conceptually track?
2. What happens when append exceeds a slice's current capacity?
3. Given arr := [5]int{10,20,30,40,50} and sub := arr[1:3], what does sub contain and what does it share with arr?
4. What is the purpose of make([]int, 0, 10)?
5. Why can mutating a slice obtained via slicing an existing slice affect the original?
Was this page helpful?
You May Also Like
Arrays in Go
A fixed-size, ordered collection of elements of the same type whose length is part of its type.
Maps in Go
An unordered collection of key-value pairs providing fast lookup, insertion, and deletion by key.
for Loops in Go
Master Go's single looping construct, which covers classic for, while-style, infinite, and range-based loops.
Pointers in Go
A variable that holds the memory address of another value, enabling shared access and mutation without copying.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics