
In Go, you can pass pointers to functions to modify the original value of variables inside the function. This technique is called pass by reference, where instead of passing the value itself, the memory address (or reference) is passed.
This allows the function to directly manipulate the original data, rather than working with a copy.
1ī¸âŖ Passing Pointers vs. Passing Values
When you pass a value to a function, Go makes a copy of the value, and any changes made to that copy inside the function do not affect the original value.
When you pass a pointer to a function, the function receives the memory address, so any changes made via the pointer will affect the original variable.
2ī¸âŖ How to Pass a Pointer to a Function
To pass a pointer, use the * (pointer) and & (address-of) operators.
đš Example:
package main
import "fmt"
// Function that takes a pointer to an int
func updateValue(ptr *int) {
*ptr = *ptr + 10 // Dereference and modify the value
}
func main() {
x := 20
fmt.Println("Before:", x) // Output: 20
updateValue(&x) // Pass the address of x
fmt.Println("After:", x) // Output: 30
}
đš Output:
Before: 20
After: 30
đ Here, updateValue receives the memory address of x and modifies the value directly via the pointer. So, the original value of x is changed.
3ī¸âŖ Why Use Pointers in Functions?
4ī¸âŖ Example with Struct
When working with structs, you can pass a pointer to modify its fields inside a function.
đš Example:
package main
import "fmt"
type Person struct {
name string
age int
}
// Function to update the age of a person
func birthday(p *Person) {
p.age = p.age + 1 // Modify struct field via pointer
}
func main() {
p := Person{"John", 30}
fmt.Println("Before:", p.age) // Output: 30
birthday(&p) // Pass the address of the struct
fmt.Println("After:", p.age) // Output: 31
}
đš Output:
Before: 30
After: 31
đ By passing a pointer to the Person struct, the function birthday modifies the age field directly.
5ī¸âŖ Passing Pointer to Arrays or Slices
Although Go doesn't allow pointer arithmetic, you can pass pointers to arrays or slices, and modify their elements directly inside functions.
đš Example:
package main
import "fmt"
// Function to update an element in an array
func updateArray(arr *[3]int) {
arr[0] = 100 // Modify the first element
}
func main() {
nums := [3]int{1, 2, 3}
fmt.Println("Before:", nums) // Output: [1 2 3]
updateArray(&nums) // Pass the address of the array
fmt.Println("After:", nums) // Output: [100 2 3]
}
đš Output:
Before: [1 2 3]
After: [100 2 3]
* operator to declare pointers and the & operator to get the address of a variable.Go āϤā§, āĻāĻĒāύāĻŋ pointer āĻĢāĻžāĻāĻļāύ⧠āĻĒāĻžāĻ āĻžāϤ⧠āĻĒāĻžāϰā§āύ, āϝāĻžāϤ⧠āĻĢāĻžāĻāĻļāύāĻāĻŋ āϏāϰāĻžāϏāϰāĻŋ āĻŽā§āϞ āĻā§āϝāĻžāϰāĻŋāϝāĻŧā§āĻŦāϞā§āϰ āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āĻāϰāϤ⧠āĻĒāĻžāϰā§āĨ¤ āĻāĻ āĻĒāĻĻā§āϧāϤāĻŋāĻā§ āĻŦāϞāĻž āĻšāϝāĻŧ pass by reference, āϝā§āĻāĻžāύ⧠āĻā§āϝāĻžāϰāĻŋāϝāĻŧā§āĻŦāϞā§āϰ āĻŽāĻžāύā§āϰ āĻĒāϰāĻŋāĻŦāϰā§āϤ⧠āϤāĻžāϰ āĻŽā§āĻŽā§āϰāĻŋ āĻ āĻŋāĻāĻžāύāĻž (āĻŦāĻž reference) āĻĒāĻžāĻ āĻžāύ⧠āĻšāϝāĻŧāĨ¤
āĻāĻāĻŋ āĻĢāĻžāĻāĻļāύāĻā§ āĻŽā§āϞ āĻĄā§āĻāĻž āϏāϰāĻžāϏāϰāĻŋ āĻŽā§āϝāĻžāύāĻŋāĻĒā§āϞā§āĻ āĻāϰāĻžāϰ āĻ āύā§āĻŽāϤāĻŋ āĻĻā§āϝāĻŧ, āĻāĻĒāĻŋ āύāĻŋāϝāĻŧā§ āĻāĻžāĻ āĻāϰāĻžāϰ āĻĒāϰāĻŋāĻŦāϰā§āϤā§āĨ¤
1ī¸âŖ Pointer āĻĒāĻžāĻ āĻžāύ⧠āĻŦāύāĻžāĻŽ āĻŽāĻžāύ āĻĒāĻžāĻ āĻžāύā§
āϝāĻāύ āĻāĻĒāύāĻŋ āĻāĻāĻāĻŋ āĻŽāĻžāύ (value) āĻĢāĻžāĻāĻļāύ⧠āĻĒāĻžāĻ āĻžāύ, Go āϏā§āĻ āĻŽāĻžāύāĻāĻŋāϰ āĻāĻāĻāĻŋ āĻāĻĒāĻŋ āϤā§āϰāĻŋ āĻāϰā§āĨ¤ āĻĢāĻžāĻāĻļāύā§āϰ āĻāĻŋāϤāϰ⧠āϏā§āĻ āĻāĻĒāĻŋāϤ⧠āĻāϰāĻž āĻā§āύ⧠āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āĻŽā§āϞ āĻŽāĻžāύāĻā§ āĻĒā§āϰāĻāĻžāĻŦāĻŋāϤ āĻāϰ⧠āύāĻžāĨ¤
āĻāĻŋāύā§āϤ⧠āϝāĻāύ āĻāĻĒāύāĻŋ āĻāĻāĻāĻŋ pointer āĻĒāĻžāĻ āĻžāύ, āĻĢāĻžāĻāĻļāύāĻāĻŋ āĻŽā§āĻŽā§āϰāĻŋ āĻ āĻŋāĻāĻžāύāĻž āĻĒāĻžāϝāĻŧ, āĻĢāϞ⧠pointer āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠āĻĢāĻžāĻāĻļāύāĻāĻŋ āĻŽā§āϞ āĻā§āϝāĻžāϰāĻŋāϝāĻŧā§āĻŦāϞā§āϰ āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āĻāϰāϤ⧠āĻĒāĻžāϰā§āĨ¤
2ī¸âŖ Pointer āĻāĻŋāĻāĻžāĻŦā§ āĻĢāĻžāĻāĻļāύ⧠āĻĒāĻžāĻ āĻžāϤ⧠āĻšāϝāĻŧ
Pointer āĻĒāĻžāĻ āĻžāϤā§, * (pointer) āĻāĻŦāĻ & (address-of) āĻ
āĻĒāĻžāϰā§āĻāϰ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāϤ⧠āĻšāϝāĻŧāĨ¤
đš āĻāĻĻāĻžāĻšāϰāĻŖ:
package main
import "fmt"
// āĻĢāĻžāĻāĻļāύ āϝāĻž āĻāĻāĻāĻŋ pointer āĻā§āϰāĻšāĻŖ āĻāϰā§
func updateValue(ptr *int) {
*ptr = *ptr + 10 // Dereference āĻāϰ⧠āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āĻāϰāĻž āĻšāϞā§
}
func main() {
x := 20
fmt.Println("āĻāĻā§:", x) // Output: 20
updateValue(&x) // x āĻāϰ āĻŽā§āĻŽā§āϰāĻŋ āĻ āĻŋāĻāĻžāύāĻž āĻĒāĻžāĻ āĻžāύ⧠āĻšāϞā§
fmt.Println("āĻĒāϰā§:", x) // Output: 30
}
đš Output:
āĻāĻā§: 20
āĻĒāϰā§: 30
đ āĻāĻāĻžāύā§, updateValue āĻĢāĻžāĻāĻļāύāĻāĻŋ x āĻāϰ āĻŽā§āĻŽā§āϰāĻŋ āĻ āĻŋāĻāĻžāύāĻž āĻĒāĻžāϝāĻŧ āĻāĻŦāĻ pointer āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠āϏāϰāĻžāϏāϰāĻŋ āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āĻāϰā§āĨ¤ āĻĢāϞ⧠x āĻāϰ āĻŽā§āϞ āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āĻšāϝāĻŧāĨ¤
3ī¸âŖ āĻā§āύ Pointer āĻĢāĻžāĻāĻļāύ⧠āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāĻŦā§āύ?
4ī¸âŖ Struct āϏāĻš āĻāĻĻāĻžāĻšāϰāĻŖ
Struct āύāĻŋāϝāĻŧā§ āĻāĻžāĻ āĻāϰāĻžāϰ āϏāĻŽāϝāĻŧ, āĻāĻĒāύāĻŋ pointer āĻĢāĻžāĻāĻļāύ⧠āĻĒāĻžāĻ āĻŋāϝāĻŧā§ āϤāĻžāϰ āĻĢāĻŋāϞā§āĻĄāĻā§āϞ⧠āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āĻāϰāϤ⧠āĻĒāĻžāϰāĻŦā§āύāĨ¤
đš āĻāĻĻāĻžāĻšāϰāĻŖ:
package main
import "fmt"
type Person struct {
name string
age int
}
// āĻĢāĻžāĻāĻļāύ āϝāĻž āĻŦā§āϝāĻā§āϤāĻŋāϰ āĻŦāϝāĻŧāϏ āĻāĻĒāĻĄā§āĻ āĻāϰā§
func birthday(p *Person) {
p.age = p.age + 1 // Pointer āĻāϰ āĻŽāĻžāϧā§āϝāĻŽā§ struct āĻāϰ āĻĢāĻŋāϞā§āĻĄ āĻĒāϰāĻŋāĻŦāϰā§āϤāύ
}
func main() {
p := Person{"John", 30}
fmt.Println("āĻāĻā§:", p.age) // Output: 30
birthday(&p) // Struct āĻāϰ āĻŽā§āĻŽā§āϰāĻŋ āĻ āĻŋāĻāĻžāύāĻž āĻĒāĻžāĻ āĻžāύ⧠āĻšāϞā§
fmt.Println("āĻĒāϰā§:", p.age) // Output: 31
}
đš Output:
āĻāĻā§: 30
āĻĒāϰā§: 31
đ āĻāĻāĻžāύā§, Person struct āĻāϰ pointer āĻĒāĻžāĻ āĻžāύ⧠āĻšāϝāĻŧā§āĻā§, āĻĢāϞ⧠birthday āĻĢāĻžāĻāĻļāύāĻāĻŋ āϏāϰāĻžāϏāϰāĻŋ age āĻĢāĻŋāϞā§āĻĄ āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āĻāϰā§āĻā§āĨ¤
5ī¸âŖ Array āĻŦāĻž Slice āĻāϰ Pointer āĻĒāĻžāĻ āĻžāύā§
āϝāĻĻāĻŋāĻ Go āϤ⧠pointer arithmetic āĻ āύā§āĻŽā§āĻĻāĻŋāϤ āύāϝāĻŧ, āϤāĻŦā§āĻ āĻāĻĒāύāĻŋ array āĻŦāĻž slice āĻāϰ pointer āĻĒāĻžāĻ āĻžāϤ⧠āĻĒāĻžāϰā§āύ āĻāĻŦāĻ āϤāĻžāĻĻā§āϰ āĻāĻĒāĻžāĻĻāĻžāύāĻā§āϞ⧠āϏāϰāĻžāϏāϰāĻŋ āĻĢāĻžāĻāĻļāύā§āϰ āĻāĻŋāϤāϰ⧠āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āĻāϰāϤ⧠āĻĒāĻžāϰā§āύāĨ¤
đš āĻāĻĻāĻžāĻšāϰāĻŖ:
package main
import "fmt"
// āĻĢāĻžāĻāĻļāύ āϝāĻž array āĻāϰ āĻāĻĒāĻžāĻĻāĻžāύ āĻāĻĒāĻĄā§āĻ āĻāϰā§
func updateArray(arr *[3]int) {
arr[0] = 100 // āĻĒā§āϰāĻĨāĻŽ āĻāĻĒāĻžāĻĻāĻžāύ āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āĻāϰāĻž āĻšāϞā§
}
func main() {
nums := [3]int{1, 2, 3}
fmt.Println("āĻāĻā§:", nums) // Output: [1 2 3]
updateArray(&nums) // Array āĻāϰ āĻ āĻŋāĻāĻžāύāĻž āĻĒāĻžāĻ āĻžāύ⧠āĻšāϞā§
fmt.Println("āĻĒāϰā§:", nums) // Output: [100 2 3]
}
đš Output:
āĻāĻā§: [1 2 3]
āĻĒāϰā§: [100 2 3]
* āĻāĻŦāĻ āĻ āĻŋāĻāĻžāύāĻž āĻĒā§āϤ⧠& āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāϤ⧠āĻšāϝāĻŧāĨ¤