There can be functions that don’t need a name in order to be defined. These are anonymous functions. Go has support for first-class functions. In this post, we will see how to use anonymous functions in the Go programming language.
What are Anonymous Functions?
Anonymous functions are those which don’t need a name to be defined. And they can be defined in the flow of a program and can immediately be invoked.
Declaring Anonymous Functions
Declaration syntax for anonymous functions is pretty straightforward. It is no different in syntax than a regular function. But it can be defined inside the flow of a function and even invoked immediately.
package main
import (
"fmt"
)
func main() {
func(){
fmt.Println("Inside anonymous function")
}
}
Assigning Anonymous Function to a Variable
An anonymous function can be assigned to a variable and be called using the variable name. Here is an example showing an anonymous function assigned to a variable.
package main
import (
"fmt"
)
func main() {
v := func(){
fmt.Println("Inside anonymous function")
}
}
Invoking Anonymous Functions
Anonymous functions, when assigned to a variable, can be called using the variable name. Also, it can be invoked immediately.
1. Immediate Invocation of Anonymous Functions
Immediate invocation happens when a function that is declared gets called immediately after declaration. Below is an example of an immediate invocation of an anonymous function.
package main
import (
"fmt"
)
func main() {
func(){
fmt.Println("This function gets invoked immediately")
}()
// prints "This function gets invoked immediately"
}
2. Invoking an Anonymous Function by Name
Anonymous functions can be invoked by using the variable name it’s assigned to. To do that use the variable name and add the parenthesis beside it. Here is how it’s done.
package main
import (
"fmt"
)
func main() {
// declare the variable
f := func(){
fmt.Println("This is an anonymous function")
}
// with parameters
g := func(n int) {
fmt.Println(n)
}
// call it
f() // prints "This is an anonymous function"
g(42) // prints 42
}
Passing Arguments to an Anonymous Function
Anonymous functions can take any number of arguments of any type just like a regular function. Let’s see an example of using different arguments with an anonymous function.
package main
import (
"fmt"
)
func main() {
func(v int){
fmt.Println(v)
}(42)
// prints 42
}
Passing Anonymous Functions as an Argument
Anonymous functions can be passed as an argument of a function as well. Here is an example of an anonymous function passed as an argument.
package main
import (
"fmt"
)
func main() {
g := func(v string){
fmt.Println(v)
}
func(v string, g func(v string)){
g(v)
}("Hello!", g)
// prints "Hello!"
}
Returning Anonymous Function from a Function
An anonymous function can be returned from a function. Let’s see an example where we will return an anonymous function from another function.
package main
import (
"fmt"
)
func main() {
f1 := func() func(v int) {
f := func(v int) {
fmt.Println(v)
}
return f
}
g := f1()
g(42) // prints 42
}
Closures in Anonymous Function
Go functions support closures. Closures happen when a function references a variable outside of its scope. Here is an example illustrating the closure.
package main
import (
"fmt"
)
func f() func() {
n := 0
return func() {
n++
fmt.Println(n)
}
}
func main() {
h0 := f()
h1 := f()
h0() // 1
h1() // 1
h0() // 2
h1() // 2
}