Go Arrays vs Slices – Key Differences and Performance Tips
Arrays in Go
An array has a fixed size defined at compile time and cannot grow or shrink.
var arr [5]int = [5]int{1, 2, 3, 4, 5}
fmt.Println(len(arr)) // 5
Slices in Go
A slice is a dynamically-sized, flexible view into an underlying array, and is far more commonly used than arrays in everyday Go code.
nums := []int{1, 2, 3}
nums = append(nums, 4) // grows dynamically
fmt.Println(nums) // [1 2 3 4]
Key Differences
- Size: arrays have a fixed size; slices can grow using
append(). - Usage: slices are far more common in idiomatic Go code due to their flexibility.
- Memory: a slice is a small struct (pointer, length, capacity) referencing an underlying array, making it efficient to pass around.
Performance Tip
Pre-allocating slice capacity with make([]int, 0, 100) avoids repeated reallocations when you know the approximate size in advance.
PreviousPointers in Go – When and Why to Use Them (Memory Management)
Next How to Use Maps in Go – Best Practices for Key-Value Data
Ready to master real-world Go skills?
Learn Go hands-on with mentor-led, live sessions.
.png)