First-Class Functions in Go

List Topics
February 15, 2025
No Comments
4 min read

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.

1️⃣ Assigning Functions to Variables (Function as a Variable)

In Go, a function can be stored in a variable and called later.

🔹 Example:

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

Bash
Sum: 30

👉 Here, an anonymous function is assigned to the variable add, which is then used like a regular function.

2️⃣ Passing Functions as Arguments (Function as an Argument)

In Go, a function can be passed as an argument to another function.

🔹 Example:

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

Bash
Result: 8
Result: 15

👉 Here, the function executeOperation accepts another function as an argument and applies it to the given numbers.

3️⃣ Returning Functions from Another Function (Function as a Return Value)

In Go, a function can return another function.

🔹 Example:

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

Bash
Result: 10

👉 Here, the function getOperation returns another function, which is then assigned to operation and called later.

4️⃣ Nested Functions (Function inside Function)

Go allows defining a function inside another function.

🔹 Example:

Go
package main

import "fmt"

func main() {
    func() {
        fmt.Println("I am a nested function!")
    }()
}

🔹 Output:

Bash
I am a nested function!

👉 Here, an anonymous function is defined inside main() and executed immediately.

Conclusion

FeatureDescription
Functions can be assigned to variablesFunctions can be stored and used like variables.
Functions can be passed as argumentsA function can take another function as a parameter.
Functions can return functionsA function can return another function.
Nested functions are allowedFunctions can be defined inside other functions.

🚀 First-Class Functions in Go make programming more flexible, reusable, and modular! 🚀

First-Class Functions in Go (Go-তে প্রথম শ্রেণির ফাংশন)

একটি ভাষায় ফাংশন যদি ভেরিয়েবলে সংরক্ষণ করা যায়, প্যারামিটার হিসেবে পাঠানো যায় এবং রিটার্ন করা যায়, তবে তাকে First-Class Function Support বলা হয়।

👉 Go একটি First-Class Function সমর্থন করে, যার মানে হলো:
✅ ফাংশনকে ভেরিয়েবলে সংরক্ষণ করা যায়।
✅ ফাংশনকে অন্য ফাংশনের প্যারামিটার হিসেবে পাঠানো যায়।
✅ ফাংশনকে অন্য ফাংশন থেকে রিটার্ন করা যায়।

1️⃣ ফাংশনকে ভেরিয়েবলে সংরক্ষণ করা (Function as a Variable)

Go-তে একটি ফাংশনকে ভেরিয়েবলে সংরক্ষণ করা যায়, এবং পরবর্তীতে সেটিকে ব্যবহার করা যায়।

🔹 উদাহরণ:

Go
package main

import "fmt"

func main() {
    // ফাংশনকে একটি ভেরিয়েবলে সংরক্ষণ করা হলো
    add := func(a, b int) int {
        return a + b
    }

    result := add(10, 20) // ফাংশন কল করা হলো
    fmt.Println("যোগফল:", result)
}

🔹 আউটপুট:

Bash
যোগফল: 30

👉 এখানে add নামের ভেরিয়েবলে একটি অ্যানোনিমাস ফাংশন (Anonymous Function) সংরক্ষণ করা হয়েছে, এবং পরবর্তীতে সেটিকে add(10, 20) হিসেবে কল করা হয়েছে।

2️⃣ ফাংশনকে প্যারামিটার হিসেবে পাঠানো (Function as an Argument)

Go-তে ফাংশনকে অন্য ফাংশনের প্যারামিটার হিসেবে পাঠানো যায়।

🔹 উদাহরণ:

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) 
}

🔹 আউটপুট:

Bash
ফলাফল: 8
ফলাফল: 15

👉 এখানে executeOperation ফাংশন একটি ফাংশনকে প্যারামিটার হিসেবে গ্রহণ করছে এবং সেটিকে প্রয়োগ করছে।

3️⃣ ফাংশনকে রিটার্ন করা (Function as a Return Value)

Go-তে একটি ফাংশন অন্য ফাংশন থেকে রিটার্ন করা যায়

🔹 উদাহরণ:

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)
}

🔹 আউটপুট:

Bash
ফলাফল: 10

👉 এখানে getOperation ফাংশন একটি ফাংশন রিটার্ন করছে, যা পরে ব্যবহার করা হয়েছে।

4️⃣ ফাংশন Nesting (Function inside Function)

Go-তে ফাংশন অন্য ফাংশনের ভেতরে ডিফাইন করা যায়

🔹 উদাহরণ:

Go
package main

import "fmt"

func main() {
    func() {
        fmt.Println("আমি একটি Nested Function!")
    }()
}

🔹 আউটপুট:

Bash
আমি একটি Nested Function!

👉 এখানে একটি ফাংশনকে অন্য ফাংশনের ভেতরে ডিফাইন করে সাথে সাথে এক্সিকিউট করা হয়েছে।


উপসংহার (Conclusion)

First-Class Function বৈশিষ্ট্যব্যাখ্যা
ফাংশনকে ভেরিয়েবলে রাখা যায়ফাংশনকে ভেরিয়েবলে সংরক্ষণ করা সম্ভব
ফাংশন প্যারামিটার হিসেবে ব্যবহার করা যায়ফাংশনকে অন্য ফাংশনের ইনপুট হিসেবে পাঠানো যায়
ফাংশনকে রিটার্ন করা যায়ফাংশন থেকে নতুন ফাংশন রিটার্ন করা যায়
Nested Functions সমর্থন করেফাংশনকে অন্য ফাংশনের ভেতরে ডিফাইন করা যায়

🚀 Go-এর First-Class Functions প্রোগ্রামিংকে আরও ডাইনামিক, রিইউজেবল এবং মডুলার করে তোলে!🚀

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