Passing Pointers to Functions in Go

List Topics
February 16, 2025
No Comments
4 min read

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.

Step-by-Step Explanation:

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:

Go
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:

Bash
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?

  • Efficiency: Passing large structs or arrays by value can be memory-intensive. Pointers allow you to pass references, which are lightweight.
  • Mutability: If you want to modify the original variable in a function, you need to pass a pointer to allow changes to be reflected outside the function.

4ī¸âƒŖ Example with Struct

When working with structs, you can pass a pointer to modify its fields inside a function.

🔹 Example:

Go
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:

Bash
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:

Go
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:

Bash
Before: [1 2 3]
After: [100 2 3]

Summary:

  • Passing pointers to functions in Go allows you to modify the original value rather than a copy.
  • Use the * operator to declare pointers and the & operator to get the address of a variable.
  • Pointers are useful for:
    • Efficiency: Passing large data structures by reference.
    • Mutability: Modifying the original value inside the function.

āĻĢāĻžāĻ‚āĻļāύ⧇ Pointer āĻĒāĻžāĻ āĻžāύ⧋ (Go āϤ⧇)

Go āϤ⧇, āφāĻĒāύāĻŋ pointer āĻĢāĻžāĻ‚āĻļāύ⧇ āĻĒāĻžāĻ āĻžāϤ⧇ āĻĒāĻžāϰ⧇āύ, āϝāĻžāϤ⧇ āĻĢāĻžāĻ‚āĻļāύāϟāĻŋ āϏāϰāĻžāϏāϰāĻŋ āĻŽā§‚āϞ āĻ­ā§āϝāĻžāϰāĻŋāϝāĻŧ⧇āĻŦāϞ⧇āϰ āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻ•āϰāϤ⧇ āĻĒāĻžāϰ⧇āĨ¤ āĻāχ āĻĒāĻĻā§āϧāϤāĻŋāϕ⧇ āĻŦāϞāĻž āĻšāϝāĻŧ pass by reference, āϝ⧇āĻ–āĻžāύ⧇ āĻ­ā§āϝāĻžāϰāĻŋāϝāĻŧ⧇āĻŦāϞ⧇āϰ āĻŽāĻžāύ⧇āϰ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤ⧇ āϤāĻžāϰ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ āĻŋāĻ•āĻžāύāĻž (āĻŦāĻž reference) āĻĒāĻžāĻ āĻžāύ⧋ āĻšāϝāĻŧāĨ¤

āĻāϟāĻŋ āĻĢāĻžāĻ‚āĻļāύāϕ⧇ āĻŽā§‚āϞ āĻĄā§‡āϟāĻž āϏāϰāĻžāϏāϰāĻŋ āĻŽā§āϝāĻžāύāĻŋāĻĒ⧁āϞ⧇āϟ āĻ•āϰāĻžāϰ āĻ…āύ⧁āĻŽāϤāĻŋ āĻĻ⧇āϝāĻŧ, āĻ•āĻĒāĻŋ āύāĻŋāϝāĻŧ⧇ āĻ•āĻžāϜ āĻ•āϰāĻžāϰ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤ⧇āĨ¤

Step-by-Step āĻŦā§āϝāĻžāĻ–ā§āϝāĻž:

1ī¸âƒŖ Pointer āĻĒāĻžāĻ āĻžāύ⧋ āĻŦāύāĻžāĻŽ āĻŽāĻžāύ āĻĒāĻžāĻ āĻžāύ⧋

āϝāĻ–āύ āφāĻĒāύāĻŋ āĻāĻ•āϟāĻŋ āĻŽāĻžāύ (value) āĻĢāĻžāĻ‚āĻļāύ⧇ āĻĒāĻžāĻ āĻžāύ, Go āϏ⧇āχ āĻŽāĻžāύāϟāĻŋāϰ āĻāĻ•āϟāĻŋ āĻ•āĻĒāĻŋ āϤ⧈āϰāĻŋ āĻ•āϰ⧇āĨ¤ āĻĢāĻžāĻ‚āĻļāύ⧇āϰ āĻ­āĻŋāϤāϰ⧇ āϏ⧇āχ āĻ•āĻĒāĻŋāϤ⧇ āĻ•āϰāĻž āϕ⧋āύ⧋ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻŽā§‚āϞ āĻŽāĻžāύāϕ⧇ āĻĒā§āϰāĻ­āĻžāĻŦāĻŋāϤ āĻ•āϰ⧇ āύāĻžāĨ¤

āĻ•āĻŋāĻ¨ā§āϤ⧁ āϝāĻ–āύ āφāĻĒāύāĻŋ āĻāĻ•āϟāĻŋ pointer āĻĒāĻžāĻ āĻžāύ, āĻĢāĻžāĻ‚āĻļāύāϟāĻŋ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ āĻŋāĻ•āĻžāύāĻž āĻĒāĻžāϝāĻŧ, āĻĢāϞ⧇ pointer āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ āĻĢāĻžāĻ‚āĻļāύāϟāĻŋ āĻŽā§‚āϞ āĻ­ā§āϝāĻžāϰāĻŋāϝāĻŧ⧇āĻŦāϞ⧇āϰ āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻ•āϰāϤ⧇ āĻĒāĻžāϰ⧇āĨ¤

2ī¸âƒŖ Pointer āĻ•āĻŋāĻ­āĻžāĻŦ⧇ āĻĢāĻžāĻ‚āĻļāύ⧇ āĻĒāĻžāĻ āĻžāϤ⧇ āĻšāϝāĻŧ

Pointer āĻĒāĻžāĻ āĻžāϤ⧇, * (pointer) āĻāĻŦāĻ‚ & (address-of) āĻ…āĻĒāĻžāϰ⧇āϟāϰ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāϤ⧇ āĻšāϝāĻŧāĨ¤

🔹 āωāĻĻāĻžāĻšāϰāĻŖ:

Go
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:

Bash
āφāϗ⧇: 20
āĻĒāϰ⧇: 30

👉 āĻāĻ–āĻžāύ⧇, updateValue āĻĢāĻžāĻ‚āĻļāύāϟāĻŋ x āĻāϰ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ āĻŋāĻ•āĻžāύāĻž āĻĒāĻžāϝāĻŧ āĻāĻŦāĻ‚ pointer āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ āϏāϰāĻžāϏāϰāĻŋ āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻ•āϰ⧇āĨ¤ āĻĢāϞ⧇ x āĻāϰ āĻŽā§‚āϞ āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻšāϝāĻŧāĨ¤

3ī¸âƒŖ āϕ⧇āύ Pointer āĻĢāĻžāĻ‚āĻļāύ⧇ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻŦ⧇āύ?

  • āĻĻāĻ•ā§āώāϤāĻž: āĻŦāĻĄāĻŧ āĻŦāĻĄāĻŧ struct āĻŦāĻž array āĻāϰ āĻ•āĻĒāĻŋ āĻĢāĻžāĻ‚āĻļāύ⧇ āĻĒāĻžāĻ āĻžāύ⧋ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ–āϰāϚ āĻ•āϰāϤ⧇ āĻĒāĻžāϰ⧇āĨ¤ Pointer āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ āϰ⧇āĻĢāĻžāϰ⧇āĻ¨ā§āϏ āĻĒāĻžāĻ āĻžāύ⧋ āĻšāϝāĻŧ, āϝāĻž āϞāĻžāχāϟāĻ“āϝāĻŧ⧇āϟāĨ¤
  • āĻŽāĻŋāωāĻŸā§‡āĻŦāĻŋāϞāĻŋāϟāĻŋ: āϝāĻĻāĻŋ āφāĻĒāύāĻŋ āĻĢāĻžāĻ‚āĻļāύ⧇āϰ āĻ­āĻŋāϤāϰ⧇ āĻŽā§‚āϞ āĻ­ā§āϝāĻžāϰāĻŋāϝāĻŧ⧇āĻŦāϞāϟāĻŋ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻ•āϰāϤ⧇ āϚāĻžāύ, āϤāĻžāĻšāϞ⧇ pointer āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāϤ⧇ āĻšāĻŦ⧇ āϝāĻžāϤ⧇ āϏ⧇āχ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻŦāĻžāχāϰ⧇ āĻĨ⧇āϕ⧇ āĻĻ⧇āĻ–āĻž āϝāĻžāϝāĻŧāĨ¤

4ī¸âƒŖ Struct āϏāĻš āωāĻĻāĻžāĻšāϰāĻŖ

Struct āύāĻŋāϝāĻŧ⧇ āĻ•āĻžāϜ āĻ•āϰāĻžāϰ āϏāĻŽāϝāĻŧ, āφāĻĒāύāĻŋ pointer āĻĢāĻžāĻ‚āĻļāύ⧇ āĻĒāĻžāĻ āĻŋāϝāĻŧ⧇ āϤāĻžāϰ āĻĢāĻŋāĻ˛ā§āĻĄāϗ⧁āϞ⧋ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŦ⧇āύāĨ¤

🔹 āωāĻĻāĻžāĻšāϰāĻŖ:

Go
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:

Bash
āφāϗ⧇: 30
āĻĒāϰ⧇: 31

👉 āĻāĻ–āĻžāύ⧇, Person struct āĻāϰ pointer āĻĒāĻžāĻ āĻžāύ⧋ āĻšāϝāĻŧ⧇āϛ⧇, āĻĢāϞ⧇ birthday āĻĢāĻžāĻ‚āĻļāύāϟāĻŋ āϏāϰāĻžāϏāϰāĻŋ age āĻĢāĻŋāĻ˛ā§āĻĄ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻ•āϰ⧇āϛ⧇āĨ¤

5ī¸âƒŖ Array āĻŦāĻž Slice āĻāϰ Pointer āĻĒāĻžāĻ āĻžāύ⧋

āϝāĻĻāĻŋāĻ“ Go āϤ⧇ pointer arithmetic āĻ…āύ⧁āĻŽā§‹āĻĻāĻŋāϤ āύāϝāĻŧ, āϤāĻŦ⧁āĻ“ āφāĻĒāύāĻŋ array āĻŦāĻž slice āĻāϰ pointer āĻĒāĻžāĻ āĻžāϤ⧇ āĻĒāĻžāϰ⧇āύ āĻāĻŦāĻ‚ āϤāĻžāĻĻ⧇āϰ āωāĻĒāĻžāĻĻāĻžāύāϗ⧁āϞ⧋ āϏāϰāĻžāϏāϰāĻŋ āĻĢāĻžāĻ‚āĻļāύ⧇āϰ āĻ­āĻŋāϤāϰ⧇ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻ•āϰāϤ⧇ āĻĒāĻžāϰ⧇āύāĨ¤

🔹 āωāĻĻāĻžāĻšāϰāĻŖ:

Go
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:

Bash
āφāϗ⧇: [1 2 3]
āĻĒāϰ⧇: [100 2 3]

āĻŽā§‚āϞ āĻŦāĻ•ā§āϤāĻŦā§āϝ:

  • Go āϤ⧇ pointer āĻĢāĻžāĻ‚āĻļāύ⧇ āĻĒāĻžāĻ āĻžāύ⧋āϰ āĻŽāĻžāĻ§ā§āϝāĻŽā§‡ āĻŽā§‚āϞ āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻ•āϰāĻž āϝāĻžāϝāĻŧ, āĻ•āĻĒāĻŋ āύāϝāĻŧāĨ¤
  • Pointer āĻĄāĻŋāĻ•ā§āϞ⧇āϝāĻŧāĻžāϰ āĻ•āϰāϤ⧇ * āĻāĻŦāĻ‚ āĻ āĻŋāĻ•āĻžāύāĻž āĻĒ⧇āϤ⧇ & āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāϤ⧇ āĻšāϝāĻŧāĨ¤
  • Pointer āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻžāϰ āϏ⧁āĻŦāĻŋāϧāĻž:
    • āĻĻāĻ•ā§āώāϤāĻž: āĻŦāĻĄāĻŧ āĻĄā§‡āϟāĻž āĻ—āĻ āύāϕ⧇ āϰ⧇āĻĢāĻžāϰ⧇āĻ¨ā§āϏ⧇āϰ āĻŽāĻžāĻ§ā§āϝāĻŽā§‡ āĻĒāĻžāϏ āĻ•āϰāĻžāĨ¤
    • āĻŽāĻŋāωāĻŸā§‡āĻŦāĻŋāϞāĻŋāϟāĻŋ: āĻĢāĻžāĻ‚āĻļāύ⧇āϰ āĻ­āĻŋāϤāϰ⧇ āĻŽā§‚āϞ āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻ•āϰāĻžāĨ¤

Š2025 Linux Bangla | Developed & Maintaind by Linux Bangla.