- Published on
Arrays vs Slices in Go: Key Differences and Best Practices
- Authors

- Name
- Mehdi Akiki
Reference
When working with Go, you’ll frequently use arrays and slices to manage lists of data. They look similar but behave quite differently, especially when passing them into functions. This article explains the key distinctions and why knowing the difference is essential for writing efficient, bug-free Go code.
The Basics: Arrays vs. Slices
- Arrays are fixed-size collections of elements. Once declared, their size cannot be changed.
- Slices, on the other hand, are dynamically-sized, making them more flexible and often more convenient. One crucial difference lies in how they handle memory. Arrays are passed by value, meaning a copy is created, while slices are passed by reference. Let’s look at what that means in practice.
Example 1: Passing Arrays by Value
In Go, when you pass an array to a function, it creates a full copy of the array. Any modifications you make within the function won’t affect the original array.
func modifyArray(arr [3]int) {
arr[0] = 10 // Modifies only the copy
}
func main() {
original := [3]int{1, 2, 3}
modifyArray(original)
fmt.Println(original) // Output: [1, 2, 3]
}
In this example, modifyArray works on a copy of original, so changes to arr within the function don’t impact the original array.
Example 2: Passing Slices by Reference
With slices, Go passes a reference to the underlying array, so modifications within the function affect the original slice.
func modifySlice(slice []int) {
slice[0] = 10 // Modifies the original slice
}
func main() {
original := []int{1, 2, 3}
modifySlice(original)
fmt.Println(original) // Output: [10, 2, 3]
}
Here, modifySlice directly modifies the original data. Because slices are references, any changes within the function are reflected in original.
Why It Matters
Understanding the difference between arrays and slices is crucial for memory management and performance optimization in Go. Use arrays when you need a fixed-size, isolated data structure. For flexible, shared data structures where you want to avoid unnecessary copying, slices are the way to go.
Key Takeaways
- Arrays are passed by value—they create copies.
- Slices are passed by reference—they modify the original data.
- Knowing when to use each can help you write more efficient and effective Go code.
Conclusion
The choice between arrays and slices may seem small, but it has significant implications. If you need data that changes dynamically or want to modify it in place, go with slices. For fixed, isolated data, arrays are a good fit. Mastering these concepts will make you a more effective Go developer, especially in performance-critical applications.