Go anonymous function

In this example, we will show you how to create an anonymous function in Go with an example.

In Go, we can create anonymous functions. Anonymous functions do not have a name.

Go anonymous function

We create an anonymous function that adds three values. We pass three parameters to the function right after its definition.


package main

import "fmt"

func main() {

    sum := func(a, b, c int) int {
        return a + b + c
    }(1, 3, 5)

    fmt.Println("1+3+5 =", sum)
}

Output:

1+3+5 = 9