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)


1+3+5 = 9

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course