📤 Go Passing Arrays to Functions – Understand Value and Reference Behavior (2025 Guide)
🧲 Introduction – Can You Pass Arrays to Functions in Go?
Yes! Go allows you to pass arrays to functions, but with an important caveat—arrays are value types, meaning they are copied when passed. If you want to avoid copying and modify the original array, you should pass a pointer or use a slice instead.
🎯 In this section, you’ll learn:
- How to pass arrays to functions in Go
- How copying affects original array data
- How to use pointers to modify arrays
- Best practices and real-world examples
✅ Basic Example – Passing an Array by Value
func modify(arr [3]int) {
arr[0] = 100
fmt.Println("Inside modify:", arr)
}
func main() {
original := [3]int{1, 2, 3}
modify(original)
fmt.Println("In main:", original)
}
📤 Output:
Inside modify: [100 2 3]
In main: [1 2 3]
✅ The function receives a copy of the array. Changes do not affect the original.
🔁 Modifying the Original – Pass by Pointer
func modify(arr *[3]int) {
arr[0] = 100
}
func main() {
original := [3]int{1, 2, 3}
modify(&original)
fmt.Println(original)
}
📤 Output:
[100 2 3]
✅ Using *[3]int allows the function to modify the original array via pointer.
🔄 Loop Through Array in Function
func printArray(arr [4]string) {
for i, v := range arr {
fmt.Printf("Index %d: %s\n", i, v)
}
}
✅ Arrays can be easily iterated and printed within functions.
📚 When to Use Slices Instead
For flexibility and performance, consider using slices:
func modifySlice(s []int) {
s[0] = 500
}
func main() {
arr := [3]int{10, 20, 30}
modifySlice(arr[:]) // Pass array as slice
fmt.Println(arr) // Output: [500 20 30]
}
✅ Slices are reference-like, allowing modification of the underlying array.
⚠️ Key Differences – Arrays vs Pointers vs Slices
| Method | Modifies Original? | Copy Overhead | Dynamic Size? |
|---|---|---|---|
| Array by Value | ❌ No | ✅ Yes | ❌ No |
| Array by Pointer | ✅ Yes | ❌ Minimal | ❌ No |
| Slice | ✅ Yes | ❌ Minimal | ✅ Yes |
🧠 Best Practices
- ✅ Use slices unless you explicitly need a fixed-size array
- ✅ Use pointers to arrays if you’re working with raw memory or low-level APIs
- ❌ Avoid passing large arrays by value due to performance cost
- ✅ Always define array size in function signatures for clarity
📌 Summary – Recap & Next Steps
Passing arrays to functions in Go is simple but requires understanding value semantics. You can choose between copying (value), modification (pointer), or flexibility (slice) based on your use case.
🔍 Key Takeaways:
- Arrays are value types—passed by copy
- Use
*[N]Tto pass a pointer and modify the original array - Use slices (
[]T) for better performance and flexibility - Function signatures must explicitly declare array size
⚙️ Next: Learn how slices enhance array handling with dynamic lengths and reference-like behavior.
❓ FAQs – Go Passing Arrays to Functions
❓ Are arrays in Go passed by value or reference?
✅ By value. They’re copied when passed to functions.
❓ How can I modify the original array in a function?
✅ Pass a pointer to the array using *[N]Type.
❓ Can I pass an array as a slice to a function?
✅ Yes. Use arr[:] to pass a slice view of the array.
❓ Is it efficient to pass large arrays by value?
❌ No. Prefer slices or pointers for performance reasons.
❓ Can I use range to modify array elements inside a function?
✅ Yes, but only if the array is passed by pointer or as a slice.
Share Now :
