3. Pass by Reference
func increment(x int) {
x++
fmt.Println(x)
// 6
}
func main() {
x := 5
increment(x)
fmt.Println(x)
// 5
}func increment(x *int) {
*x++
fmt.Println(*x)
// 6
}
func main() {
x := 5
increment(&x)
fmt.Println(x)
// 6
}Fields of Pointers
Pass by Value (Default)
Pass by Reference (Using Pointers)
Visual Comparison
Real Example: Updating a User
Accessing Struct Fields Through Pointers
The Simple Way (Recommended)
What's Really Happening (Long Form)
Complete Example
Accessing Multiple Fields
Common Mistake: Don't Double Dereference
When to Use Pointers in Functions
Quick Reference
Scenario
Syntax
Result