Anonymous Functions in Go

Anonymous Functions in Go

ยท

3 min read

The anonymous function is a feature in Golang which let us define a function without a name. This feature is also called a function literal. This is useful when you want an inline function or to form a closure.

Declaring the anonymous function

The syntax is pretty straightforward and much similar to normal function.

func(parameter_list)(return_type){
  return
}()

Parameter list and return type are optional.

() this will invoke the function as soon as it is defined.

package main

import "fmt"

func main() {
    func() {
        fmt.Println("Golang Rocks!")
    }()
}

Run this code in Go Playground

Assigning an anonymous function to a variable

In Go, you can assign an anonymous function to a variable. The assigned variable will be of function type and it can be called as a regular function.

package main

import "fmt"

func main() {
    v := func() {
        fmt.Println("Golang Rocks!")
    }
    fmt.Printf("Type of variable v: %T\n", v)
    v()
}
Type of variable v: func()
Golang Rocks!

Run this code in Go Playground

Passing an argument to an anonymous function

An anonymous function can take any number of arguments similar to a regular function.

package main

import "fmt"

func main() {
    func(name string) {
        fmt.Println("Hello, ", name)
    }("Jack")
}

Run this code in Go Playground

With any number of trailing arguments similar to Variadic functions

package main

import "fmt"

func main() {
    func(i ...int) {
        fmt.Println(i)
    }(1, 2, 3, 4)
}

Run this code in Go Playground

Passing an anonymous function as an argument

You can pass an anonymous function as an argument to a regular function or an anonymous function.

  • Anonymous functions as an argument to a regular function
package main

import "fmt"

func sayHello(af func(s string) string) {
    fmt.Println(af("Jack"))
}

func main() {

    sayHello(func(s string) string {
        return "Hello, " + s
    })
}

Run this code in Go Playground

  • Anonymous function as an argument to an anonymous function
package main

import "fmt"

func main() {
    func(v string) {
        fmt.Println(v)
    }(func(s string) string {
        return "Hello, " + s
    }("Jack"))
}

Run this code in Go Playground

This above code looks a bit complicated and hard to fathom so another way we can achieve this is the following:

package main

import "fmt"

func main() {
    af := func(s string) string {
        return "Hello, " + s
    }

    func(af func(s string) string) {
        fmt.Println(af("Jack"))
    }(af)
}

Run this code in Go Playground

Return an anonymous function from another function

We can return an anonymous function from another function

  • Returning from regular function
package main

import "fmt"

func sayHello() func(s string) string {
    r := func(s string) string {
        return "Hello, " + s
    }
    return r
}

func main() {
    f := sayHello()
    fmt.Printf("Type of variable f: %T\n", f)
    fmt.Println(f("Jack"))
}

Run this code in Go Playground

  • Returning from the anonymous function
package main

import "fmt"

func main() {
    f := func() func(s string) string {
        r := func(s string) string {
            return "Hello, " + s
        }
        return r
    }

    fmt.Printf("Type of variable f: %T\n", f)
    c := f()

    fmt.Printf("Type of variable c: %T\n", c)
    fmt.Println(c("Jack"))
}

Run this code in Go Playground

Thank you for reading this blog, and please give your feedback in the comment section below.

Did you find this article valuable?

Support Pratik Jagrut by becoming a sponsor. Any amount is appreciated!

ย