Go Pointers & Memory – Complete Guide with Dereferencing, Function Use, and Pointer Arrays
Introduction – Why Pointers Matter in Go?
Pointers in Go give you direct access to memory addresses—allowing you to modify values efficiently without copying data. They are essential for writing performant, memory-safe programs, especially when passing large structs or interacting with low-level system logic.
In this guide, you’ll learn:
- How to declare and use pointers in Go
- How to dereference and update values using pointers
- How to pass pointers to functions
- How pointer-to-pointer and arrays of pointers work
- Real-world memory control techniques
Topics Covered
| Pointer Concept | Description |
|---|---|
| Go Pointers & Dereferencing | Get and update variable values using * and & |
| Go Passing Pointers to Functions | Modify original values from inside a function |
| Go Pointer to Pointer | Pointer that stores address of another pointer |
| Go Array of Pointers | Use pointer references in array collections |
Go – Pointers & Dereferencing
Declare & Assign
var a int = 10
var p *int = &a // p points to a
fmt.Println("Value of a:", *p) // dereferencing
| Symbol | Description |
|---|---|
& | Address-of operator |
* | Dereference (get value at address) |
Updating through pointer:
*p = 20 // a is now 20
Go – Passing Pointers to Functions
func update(val *int) {
*val += 10
}
func main() {
num := 5
update(&num)
fmt.Println(num) // 15
}
Use pointers to avoid copying large data or to mutate values directly inside functions.
Go – Pointer to Pointer
var a int = 50
var p *int = &a
var pp **int = &p // pointer to pointer
fmt.Println(**pp) // prints 50
Use when managing complex memory structures, such as:
- Indirect updates
- Managing layers of references
- Simulating reference to references (common in C)
Go – Array of Pointers
arr := [3]int{10, 20, 30}
ptrArr := [3]*int{&arr[0], &arr[1], &arr[2]}
for i := 0; i < 3; i++ {
fmt.Println(*ptrArr[i])
}
Used in:
- Struct arrays
- Managing collections of references
- Optimizing memory usage without copying values
Pointers vs Non-Pointers in Go – Quick Comparison
| Feature | By Value (Non-pointer) | By Pointer (*Type) |
|---|---|---|
| Copies data? | Yes | No (shares memory) |
| Allows modification? | No | Yes |
| Use case | Constants, simple vars | Structs, big data |
| Performance | Slower (copy cost) | Faster (reference) |
Summary – Recap & Next Steps
Pointers in Go offer explicit memory control with the safety of compile-time checks. They’re ideal for performance tuning, avoiding large copies, and controlling how data is shared or updated.
Key Takeaways:
*dereferences,&retrieves address- Pointers can be passed to functions for mutation
- Arrays of pointers and pointer-to-pointer support complex data structures
- Go restricts unsafe memory access with strong type checking
Real-World Applications:
- Optimizing large structs in APIs
- Managing graph/tree structures
- Memory-safe concurrency (with care)
Frequently Asked Questions
Can I create a pointer to a pointer in Go?
Yes:
var a int = 10
var p *int = &a
var pp **int = &p
Are pointers mandatory in Go?
No. But they’re useful for efficient data sharing and mutation.
What’s the zero value of a pointer in Go?
nil—a pointer that points to no memory.
Can I pass a pointer to a slice or map?
Usually unnecessary—slices and maps are reference types by default.
Can I dereference nil in Go?
No. Dereferencing a nil pointer will cause a runtime panic.
Share Now :
