Golang Anonymous Functions: A Guide [#Go101]
Tags: go, backend, beginners
Published: 2024-02-21 22:42:51

### **Introduction**

Anonymous functions (lambda functions or literal functions) are those that are not bound to an identifier. In Golang, they are primarily used to defer tasks or execute them concurrently; to pass functions as arguments, among other use cases that we will cover today.

### **Use Cases**

- Functions as Arguments

When a higher-order function takes another function as a parameter, it is common to see anonymous functions passed as parameters to these. This is also called "**callbacks**."

```go
func forEach(numbers []int, f func(int)) {
  for _, num := range numbers {
    f(num)
  }
}

func main() {
    forEach([]int{1, 2, 3}, func(i int) {
        fmt.Printf("%d ", i * i) // Output: 1 4 9
    })
}

```
Run this code: {% embed https://go.dev/play/p/uZ0QxA0DObz %}

- Concurrent Executions (Goroutines)

Another common use is when launching Goroutines, especially when the function is simple and only used in that place. This allows us to keep the logic close to where it is used, improving readability.

```go
func main() {
    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
        // Code running concurrently
        fmt.Println("Running in a goroutine")
        wg.Done()
    }()
    wg.Wait()

    fmt.Println("Running in the main function")
}
```
Run this code: {% embed https://go.dev/play/p/ELGG4mFCyqy %}

WARNING: Very long anonymous functions may have the opposite effect on readability, making it challenging to understand the code.

- Deferring Tasks

When we want to defer the execution of more than one function, we generally use anonymous functions.

```go
func main() {
    now := time.Now()
    defer func() {
        duration := time.Since(now)
        fmt.Printf("It took %v to run this", duration)
    }()

    for i := range 1_001 {
        if i%100 == 0 {
            fmt.Printf("%d ", i)
            time.Sleep(time.Millisecond * 100)
        }
    }
    fmt.Printf("\n")
}
// Output:
// 0 100 200 300 400 500 600 700 800 900 1000
// It took 1.1s to run this
```
Run this code: {% embed https://go.dev/play/p/8iAbd9aI8Dv %}

- Closures

Anonymous functions can capture variables within their scope and maintain access to them even when the outer or parent function has already returned. This allows us to create functions that "remember" state.

```go
func makeMultiplier(factor int) func(int) int {
    return func(x int) int {
        return x * factor
    }
}

func main() {
    multiplyByTwo := makeMultiplier(2)
    r1 := multiplyByTwo(2)
    fmt.Println(r1) // Output: 4
    r2 := multiplyByTwo(5)
    fmt.Println(r2) // Output: 10
}

```
Run this code: {% embed https://go.dev/play/p/6tnfPMcsvYp %}

**WARNING**: Despite their potential, one must know when and how to use closures because they can bring issues of memory usage, readability, bugs when handling that "state," especially concurrently, or difficulties in debugging.

- Testing

We often use anonymous functions to run "subtests" in Table-Driven Tests:

```go
func Add(a, b int) int {
    return a + b
}

func TestAdd(t *testing.T) {
    testCases := []struct {
        a, b, expected int
    }{
        {1, 2, 3},
        {0, 0, 0},
        {-1, 1, 0},
    }

    for _, tc := range testCases {
        t.Run(fmt.Sprintf("%d+%d", tc.a, tc.b), func(t *testing.T) {
            result := Add(tc.a, tc.b)
            if result != tc.expected {
                t.Errorf("Expected %d, got %d", tc.expected, result)
            }
        })
    }
}

```

Returning to closures, the following is a pattern I borrowed from [Mat Ryer] (https://twitter.com/matryer) in one of his [talks at GopherCon UK](https://youtu.be/yeetIgNeIkc?si=DZiUTlqOSE-k-Om9&t=600) that I highly recommend.

```go
func TestSomething(t *testing.T) {
    file, teardown, err := setup(t)
    defer teardown()
    if err != nil {
        t.Error("setup:", err)
    }
    // do something with the file
}

func setup(t *testing.T) (*os.File, func(), error) {
    teardown := func() {}
    // create test file
    file, err := os.CreateTemp(os.TempDir(), "test")
    if err != nil {
        return nil, teardown, err
    }

    teardown = func() {
        // close file
        err := file.Close()
        if err != nil {
            t.Error("setup: Close:", err)
        }
        // remove test file
        err = os.RemoveAll(file.Name())
        if err != nil {
            t.Error("setup: RemoveAll:", err)
        }
    }
    return file, teardown, nil
}

```

### **Pros and Cons**

- Pros:

1. Conciseness: Allows you to define and use functions directly where you need them, reducing clutter in your code and the size of your internal or private APIs.
2. Readability (for simple tasks): Anonymous functions can make the code easier to read by keeping it close to where it is used.

- Cons:

1. Testability and debugging: As they are not explicitly defined and named, they can be more challenging to test and debug.
2. Readability (for long or complex tasks): When this happens, the code can become harder to understand.

### **Conclusions**

Anonymous functions can be a powerful tool, but one must choose carefully when to use them. In summary, you can use them when their logic is simple and short, and it will not be reused; and/or when you need to make use of closures.

In cases where their logic becomes very complex or the function will be reused elsewhere, consider defining it as a named function for greater maintainability and clarity.