🧷 Go Dereferencing Pointer – Access and Modify Values via Address (2025 Guide)
🧲 Introduction – What Is Dereferencing in Go?
In Go, dereferencing means accessing or modifying the value stored at a memory address pointed to by a pointer. Using the * operator, you can read or update the actual value instead of working with a copy. Dereferencing is essential for pointer-based operations like in-place modification, struct manipulation, and reference sharing.
🎯 In this section, you’ll learn:
- How to dereference a pointer in Go
- Read and update values via pointers
- Work with structs, arrays, and functions
- Best practices and error prevention
✅ Basic Syntax – Dereferencing an int Pointer
x := 10
p := &x // pointer to x
fmt.Println(*p) // dereference pointer: Output → 10
✅ *p reads the value stored at the address held by p.
🔄 Modify Value via Dereferencing
*p = 20
fmt.Println(x) // Output → 20
✅ Changing *p also updates the value of x, since p points to x.
🧩 Example – Dereferencing Struct Pointers
type Person struct {
Name string
}
p := Person{Name: "Alice"}
ptr := &p
(*ptr).Name = "Bob"
fmt.Println(p.Name) // Output → Bob
✅ You can access fields through (*ptr).Field or shorthand:
ptr.Name = "Charlie" // Same as (*ptr).Name
🔢 Dereferencing Pointer to Array
arr := [3]int{1, 2, 3}
p := &arr
(*p)[1] = 99
fmt.Println(arr) // Output → [1 99 3]
✅ Modifies the original array via its pointer.
🔁 In Functions – Dereferencing to Modify Arguments
func update(val *int) {
*val = 100
}
x := 50
update(&x)
fmt.Println(x) // Output → 100
✅ Dereferencing inside the function lets you modify the caller’s original variable.
⚠️ Common Mistakes to Avoid
| Mistake | Problem | Fix |
|---|---|---|
Dereferencing nil pointer | ❌ Runtime panic | ✅ Check if pointer is not nil |
Forgetting * in modification | ❌ No change to original | ✅ Use *p = newValue |
Using * unnecessarily on non-pointers | ❌ Compile error | ✅ Use * only on pointer types |
📦 Summary Table – Pointer and Dereference
| Symbol | Description | Example |
|---|---|---|
& | Address-of operator | p := &x |
* | Dereference (get/set the value) | *p = 20, fmt.Println(*p) |
📌 Summary – Recap & Next Steps
Dereferencing lets you work with actual data via pointers, not just their copies. It’s crucial for memory-safe, efficient Go programming in both simple and advanced use cases like structs, function arguments, and pointer arrays.
🔍 Key Takeaways:
- Use
*pto read or write the value a pointer references *is the dereference operator- Use in structs, arrays, functions, and pointer chains
- Always check for
nilpointers before dereferencing
⚙️ Next: Explore Pointer Receivers in Methods or Go Interfaces with Pointer Types.
❓ FAQs – Go Dereferencing Pointer
❓ What does *p mean in Go?
✅ It dereferences the pointer p, i.e., gets the value stored at the address.
❓ Can I use *p = value to change the original variable?
✅ Yes. This modifies the value at the referenced memory location.
❓ Is * used for both declaration and dereferencing?
✅ Yes, but in different contexts:
*intdeclares a pointer toint*paccesses the value from a pointer
❓ Can I dereference nil in Go?
❌ No. It will cause a runtime panic. Always check before dereferencing.
❓ How do I dereference a struct pointer?
✅ Use (*p).Field or the shorthand p.Field.
Share Now :
