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.