In Go, functions are first-class citizens, meaning they can be:
✅ Assigned to variables.
✅ Passed as arguments to other functions.
✅ Returned from other functions.
This allows for more dynamic, reusable, and modular programming.
In Go, a function can be stored in a variable and called later.
package main
import "fmt"
func main() {
// Storing a function in a variable
add := func(a, b int) int {
return a + b
}
result := add(10, 20) // Calling the function
fmt.Println("Sum:", result)
}
🔹 Output:
Sum: 30
👉 Here, an anonymous function is assigned to the variable add
, which is then used like a regular function.
In Go, a function can be passed as an argument to another function.
package main
import "fmt"
// Function that takes another function as a parameter
func executeOperation(operation func(int, int) int, x int, y int) {
result := operation(x, y)
fmt.Println("Result:", result)
}
func main() {
// Defining two functions
add := func(a, b int) int { return a + b }
multiply := func(a, b int) int { return a * b }
// Passing functions as arguments
executeOperation(add, 5, 3)
executeOperation(multiply, 5, 3)
}
🔹 Output:
Result: 8
Result: 15
👉 Here, the function executeOperation
accepts another function as an argument and applies it to the given numbers.
In Go, a function can return another function.
package main
import "fmt"
// Function that returns another function
func getOperation(op string) func(int, int) int {
if op == "add" {
return func(a, b int) int { return a + b }
} else if op == "multiply" {
return func(a, b int) int { return a * b }
}
return nil
}
func main() {
operation := getOperation("add") // "add" function is returned
result := operation(7, 3) // Calling the returned function
fmt.Println("Result:", result)
}
🔹 Output:
Result: 10
👉 Here, the function getOperation
returns another function, which is then assigned to operation
and called later.
Go allows defining a function inside another function.
package main
import "fmt"
func main() {
func() {
fmt.Println("I am a nested function!")
}()
}
🔹 Output:
I am a nested function!
👉 Here, an anonymous function is defined inside main()
and executed immediately.
Feature | Description |
---|---|
Functions can be assigned to variables | Functions can be stored and used like variables. |
Functions can be passed as arguments | A function can take another function as a parameter. |
Functions can return functions | A function can return another function. |
Nested functions are allowed | Functions can be defined inside other functions. |
🚀 First-Class Functions in Go make programming more flexible, reusable, and modular! 🚀
একটি ভাষায় ফাংশন যদি ভেরিয়েবলে সংরক্ষণ করা যায়, প্যারামিটার হিসেবে পাঠানো যায় এবং রিটার্ন করা যায়, তবে তাকে First-Class Function Support বলা হয়।
👉 Go একটি First-Class Function সমর্থন করে, যার মানে হলো:
✅ ফাংশনকে ভেরিয়েবলে সংরক্ষণ করা যায়।
✅ ফাংশনকে অন্য ফাংশনের প্যারামিটার হিসেবে পাঠানো যায়।
✅ ফাংশনকে অন্য ফাংশন থেকে রিটার্ন করা যায়।
Go-তে একটি ফাংশনকে ভেরিয়েবলে সংরক্ষণ করা যায়, এবং পরবর্তীতে সেটিকে ব্যবহার করা যায়।
package main
import "fmt"
func main() {
// ফাংশনকে একটি ভেরিয়েবলে সংরক্ষণ করা হলো
add := func(a, b int) int {
return a + b
}
result := add(10, 20) // ফাংশন কল করা হলো
fmt.Println("যোগফল:", result)
}
🔹 আউটপুট:
যোগফল: 30
👉 এখানে add
নামের ভেরিয়েবলে একটি অ্যানোনিমাস ফাংশন (Anonymous Function) সংরক্ষণ করা হয়েছে, এবং পরবর্তীতে সেটিকে add(10, 20)
হিসেবে কল করা হয়েছে।
Go-তে ফাংশনকে অন্য ফাংশনের প্যারামিটার হিসেবে পাঠানো যায়।
package main
import "fmt"
// ফাংশন প্যারামিটার হিসেবে গ্রহণ করা হচ্ছে
func executeOperation(operation func(int, int) int, x int, y int) {
result := operation(x, y)
fmt.Println("ফলাফল:", result)
}
func main() {
// দুটি ফাংশন তৈরি করা
add := func(a, b int) int { return a + b }
multiply := func(a, b int) int { return a * b }
// ফাংশন পাঠিয়ে executeOperation কল করা হচ্ছে
executeOperation(add, 5, 3)
executeOperation(multiply, 5, 3)
}
🔹 আউটপুট:
ফলাফল: 8
ফলাফল: 15
👉 এখানে executeOperation
ফাংশন একটি ফাংশনকে প্যারামিটার হিসেবে গ্রহণ করছে এবং সেটিকে প্রয়োগ করছে।
Go-তে একটি ফাংশন অন্য ফাংশন থেকে রিটার্ন করা যায়।
package main
import "fmt"
// একটি ফাংশন রিটার্ন করা হচ্ছে
func getOperation(op string) func(int, int) int {
if op == "add" {
return func(a, b int) int { return a + b }
} else if op == "multiply" {
return func(a, b int) int { return a * b }
}
return nil
}
func main() {
operation := getOperation("add") // "add" ফাংশন রিটার্ন হবে
result := operation(7, 3) // ফাংশন কল করা হচ্ছে
fmt.Println("ফলাফল:", result)
}
🔹 আউটপুট:
ফলাফল: 10
👉 এখানে getOperation
ফাংশন একটি ফাংশন রিটার্ন করছে, যা পরে ব্যবহার করা হয়েছে।
Go-তে ফাংশন অন্য ফাংশনের ভেতরে ডিফাইন করা যায়।
package main
import "fmt"
func main() {
func() {
fmt.Println("আমি একটি Nested Function!")
}()
}
🔹 আউটপুট:
আমি একটি Nested Function!
👉 এখানে একটি ফাংশনকে অন্য ফাংশনের ভেতরে ডিফাইন করে সাথে সাথে এক্সিকিউট করা হয়েছে।
First-Class Function বৈশিষ্ট্য | ব্যাখ্যা |
---|---|
ফাংশনকে ভেরিয়েবলে রাখা যায় | ফাংশনকে ভেরিয়েবলে সংরক্ষণ করা সম্ভব |
ফাংশন প্যারামিটার হিসেবে ব্যবহার করা যায় | ফাংশনকে অন্য ফাংশনের ইনপুট হিসেবে পাঠানো যায় |
ফাংশনকে রিটার্ন করা যায় | ফাংশন থেকে নতুন ফাংশন রিটার্ন করা যায় |
Nested Functions সমর্থন করে | ফাংশনকে অন্য ফাংশনের ভেতরে ডিফাইন করা যায় |
🚀 Go-এর First-Class Functions প্রোগ্রামিংকে আরও ডাইনামিক, রিইউজেবল এবং মডুলার করে তোলে!🚀