🧮 Go Array of Pointers – Efficiently Manage References in Arrays (2025 Guide)
🧲 Introduction – What Is an Array of Pointers in Go?
In Go, an array of pointers is a collection where each element is a pointer to a value rather than the value itself. This allows efficient memory use, in-place modification, and shared references, especially useful with structs, large data, or dynamic behavior simulations.
🎯 In this section, you’ll learn:
- How to declare and initialize an array of pointers
- How to access and modify values through pointers
- Real-world use cases like struct referencing
- Best practices and memory tips
✅ Declare an Array of Pointers
var arr [3]*int
✅ This declares an array of three pointers to integers.
🧪 Initialize an Array of Pointers
a, b, c := 10, 20, 30
arr := [3]*int{&a, &b, &c}
fmt.Println(*arr[0], *arr[1], *arr[2]) // Output: 10 20 30
✅ Each element points to an integer. *arr[i]
gives the value at that address.
🔁 Update Values via Pointers in the Array
*arr[1] = 99
fmt.Println(b) // Output: 99
✅ Changing through the pointer updates the original variable.
📦 Example – Array of Struct Pointers
type User struct {
Name string
}
u1 := User{"Alice"}
u2 := User{"Bob"}
u3 := User{"Charlie"}
users := [3]*User{&u1, &u2, &u3}
for _, u := range users {
fmt.Println(u.Name)
}
📤 Output:
Alice
Bob
Charlie
✅ This is common in object referencing, entity tracking, and dynamic updates.
🛠️ Modify Structs via Pointers in Array
users[0].Name = "Alicia"
fmt.Println(u1.Name) // Output: Alicia
✅ Changes to the struct pointer affect the original object.
🔄 Loop with Indexing
for i := range arr {
*arr[i] += 1
}
✅ Modify each pointed-to value inside the array.
⚖️ Array of Pointers vs Pointer to Array
Concept | Syntax | Meaning |
---|---|---|
Array of pointers | [3]*int | Each element is a pointer to a value |
Pointer to array | *[3]int | A single pointer to the entire array |
🧠 Best Practices
- ✅ Use array of pointers when you need to share and modify original data
- ✅ Always check for nil before dereferencing (
if arr[i] != nil
) - ❌ Avoid overusing if simple slices or arrays suffice
- ✅ Ideal for structs, linked data, tree nodes, etc.
📌 Summary – Recap & Next Steps
Arrays of pointers in Go are useful for managing shared references and enabling in-place modification of underlying data. They help reduce memory usage and increase flexibility in complex data structures.
🔍 Key Takeaways:
[*T]
stores addresses, not values- Use
*arr[i]
to access or update values - Great for working with structs and large values
- Don’t confuse with pointer to an array (
*[N]T
)
⚙️ Next: Explore Slices of Pointers or Pointers to Structs in Functions.
❓ FAQs – Go Array of Pointers
❓ What is an array of pointers in Go?
✅ An array where each element stores the address of a value, not the value itself.
❓ How do I access the value from an array of pointers?
✅ Use dereferencing: *arr[i]
.
❓ Is it safe to store struct pointers in an array?
✅ Yes. It’s commonly used for shared data and in-place updates.
❓ Can I modify the original value through the pointer array?
✅ Absolutely. You’re working with the actual memory address.
❓ What’s the difference between [*T]
and *[N]T
?
✅ [*T]
is an array of pointers. *[N]T
is a pointer to an array of values.
Share Now :