🔗 Go Call by Reference – Modify Variables with Pointers in Go (2025 Guide)
🧲 Introduction – What Is Call by Reference in Go?
Go does not support call by reference directly, but you can achieve the same effect using pointers. By passing a pointer to a function, the function can modify the original value, mimicking the behavior of call by reference found in languages like C++ or Python (with mutable types).
🎯 In this section, you’ll learn:
- How to simulate call by reference using pointers in Go
- Syntax for declaring and using pointers in functions
- Examples with numbers, structs, and arrays
- Best practices for safe and effective pointer use
📌 Go Passes Everything by Value – Including Pointers
Go always passes values by value, including pointer values. But if you pass a pointer to a variable, then changes to that pointer’s target reflect in the original variable.
✅ Example – Call by Reference Using Pointer
func modify(x *int) {
*x = *x + 10
}
func main() {
num := 5
modify(&num)
fmt.Println("Modified:", num)
}
📤 Output:
Modified: 15
✅ The *x dereferences the pointer and updates the actual value stored in num.
🧱 Example – Call by Reference with Struct
type Person struct {
name string
}
func rename(p *Person) {
p.name = "Updated"
}
func main() {
user := Person{name: "Original"}
rename(&user)
fmt.Println("Name:", user.name)
}
📤 Output:
Name: Updated
✅ Passing a pointer to a struct lets the function modify its fields directly.
🧮 Example – Update Array via Slice Pointer
func updateSlice(s *[]int) {
(*s)[0] = 999
}
func main() {
nums := []int{1, 2, 3}
updateSlice(&nums)
fmt.Println(nums)
}
📤 Output:
[999 2 3]
✅ You can even manipulate slices by passing a pointer to the slice.
🔁 Pointers vs. Values – Key Differences
| Feature | By Value (x int) | By Reference (x *int) |
|---|---|---|
| Mutates original? | ❌ No | ✅ Yes |
| Memory copied? | ✅ Yes | ⚠️ Just address |
| Use cases | Read-only | In-place update |
🧠 When to Use Pointers (Call by Reference)
Use pointer-based reference when:
- You need to modify the original variable
- You want to avoid large data copying (like with big structs)
- You’re working with linked structures or shared states
⚠️ Common Mistakes
| Mistake | Fix |
|---|---|
Using * instead of & in main | Use &var when passing to pointer param |
| Dereferencing nil pointer | Always ensure pointer is not nil |
| Forgetting to dereference | Use *pointer to access value |
📌 Summary – Recap & Next Steps
While Go uses call by value by default, you can simulate call by reference using pointers. This is critical when building APIs that modify input, manage shared state, or optimize performance for large data structures.
🔍 Key Takeaways:
- Go doesn’t have built-in call by reference, but pointers simulate it
- Pass pointers (
*Type) to modify originals from inside functions - Use
&to pass the address of a variable - Pointers improve performance and reduce memory copying
⚙️ Next: Dive into Go Functions and see how to return multiple values, work with variadic functions, and handle error returns.
❓ FAQs – Go Call by Reference
❓ Does Go support call by reference?
✅ Not natively. But Go allows you to simulate it by passing pointers.
❓ How do I change a value in a function?
✅ Pass the variable’s address using & and accept it as *Type in the function.
❓ What does *x mean in Go?
✅ It dereferences the pointer x and accesses the actual value it points to.
❓ Are slices and maps passed by reference in Go?
✅ Not exactly. Slices and maps hold references internally, so changes affect the original—but the outer variable itself is still passed by value.
❓ Is it better to always use pointers?
✅ No. Use pointers only when mutating data or avoiding large copies. Overuse can lead to confusing code and bugs.
Share Now :
