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]
*
এবং ঠিকানা পেতে &
ব্যবহার করতে হয়।