Go Anonymous Functions

Introduction

Anonymous functions in Go are functions that are defined without a name. They are useful for creating inline functions, which can be used as function literals, passed as arguments to other functions, or assigned to variables. In this chapter, you will learn the syntax and usage of anonymous functions in Go, with examples to illustrate their practical applications.

Syntax

An anonymous function is defined using the func keyword, followed by the parameters and body of the function. Since it does not have a name, it can be immediately invoked or assigned to a variable.

Syntax:

func(parameters) returnType {
    // function body
}

Examples

Basic Anonymous Function

An anonymous function can be defined and immediately invoked.

Example:

package main

import "fmt"

func main() {
    func() {
        fmt.Println("Hello, World!")
    }() // Output: Hello, World!
}

Anonymous Function with Parameters

An anonymous function can accept parameters and be immediately invoked with arguments.

Example:

package main

import "fmt"

func main() {
    func(name string) {
        fmt.Printf("Hello, %s!\n", name)
    }("Alice") // Output: Hello, Alice!
}

Assigning Anonymous Function to a Variable

An anonymous function can be assigned to a variable and invoked later.

Example:

package main

import "fmt"

func main() {
    greet := func(name string) {
        fmt.Printf("Hello, %s!\n", name)
    }
    greet("Bob") // Output: Hello, Bob!
    greet("Charlie") // Output: Hello, Charlie!
}

Anonymous Function with Return Values

An anonymous function can return values just like regular functions.

Example:

package main

import "fmt"

func main() {
    add := func(a, b int) int {
        return a + b
    }
    result := add(3, 4)
    fmt.Println("Result:", result) // Output: Result: 7
}

Using Anonymous Functions as Function Arguments

Anonymous functions can be passed as arguments to other functions.

Example:

package main

import "fmt"

func apply(operation func(int, int) int, a, b int) int {
    return operation(a, b)
}

func main() {
    result := apply(func(x, y int) int {
        return x * y
    }, 3, 4)
    fmt.Println("Multiplication Result:", result) // Output: Multiplication Result: 12
}

Anonymous Functions with Closures

Anonymous functions can capture and use variables from their surrounding scope, creating closures.

Example:

package main

import "fmt"

func main() {
    x := 10
    increment := func() int {
        x++
        return x
    }
    fmt.Println(increment()) // Output: 11
    fmt.Println(increment()) // Output: 12
}

Conclusion

Anonymous functions in Go are powerful tools for creating inline, reusable code blocks. They can be used immediately, assigned to variables, passed as arguments, and even capture variables from their surrounding context. By understanding how to define and use anonymous functions, you can write more flexible and concise code. Whether you are working with function literals, closures, or higher-order functions, anonymous functions are an essential feature in Go’s functional programming capabilities.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top