Anonymous functions are functions without a name. They are useful for creating inline functions, assigning functions to variables, and passing functions as arguments to other functions. Anonymous functions can also form closures, allowing them to access variables from their surrounding scope. Example:
package main
import "fmt"
func main() {
// Anonymous function
func() {
fmt.Println("Welcome! to GeeksforGeeks")
}()
}
Output
Welcome! to GeeksforGeeks
Syntax
func(parameterList) returnType {
// function body
}
Components:
- parameterList: Input values accepted by the function (optional).
- returnType: Data type returned by the function (optional).
- Anonymous functions can be executed immediately, assigned to variables, passed as arguments, or returned from other functions.
Immediately Invoked Anonymous Function
To execute an anonymous function immediately, add () after its definition.
Example: The following example creates and executes an anonymous function immediately.
package main
import "fmt"
func main() {
func() {
fmt.Println("Hello, Go!")
}()
}
Output
Hello, Go!
Explanation:
- func() {} creates an anonymous function.
- () at the end immediately executes the function.
- "Hello, Go!" is printed to the console.
Assigning to a Variable
An anonymous function can be assigned to a variable and called like a regular function.
package main
import "fmt"
func main() {
// Assigning an anonymous function to a variable
value := func() {
fmt.Println("Welcome! to GeeksforGeeks")
}
value()
}
Output
Welcome! to GeeksforGeeks
Passing Arguments
Anonymous functions can accept arguments.
package main
import "fmt"
func main() {
// Passing arguments in anonymous function
func(ele string) {
fmt.Println(ele)
}("GeeksforGeeks")
}
Output
GeeksforGeeks
Passing as Arguments
You can also pass an anonymous function as an argument to another function.
package main
import "fmt"
// Passing anonymous function as an argument
func GFG(i func(p, q string) string) {
fmt.Println(i("Geeks", "for"))
}
func main() {
value := func(p, q string) string {
return p + q + "Geeks"
}
GFG(value)
}
Output
GeeksforGeeks
Returning Anonymous Functions
You can return an anonymous function from another function.
package main
import "fmt"
// Returning anonymous function
func GFG() func(i, j string) string {
myf := func(i, j string) string {
return i + j + "GeeksforGeeks"
}
return myf
}
func main() {
value := GFG()
fmt.Println(value("Welcome ", "to "))
}
Output
Welcome to GeeksforGeeks
Closures
Anonymous functions can form closures. A closure is a function that can access and modify variables from its surrounding scope, even after the outer function has finished executing.
package main
import "fmt"
func counter() func() int {
count := 0
return func() int {
count++
return count
}
}
func main() {
next := counter()
fmt.Println(next())
fmt.Println(next())
fmt.Println(next())
}
Output
1 2 3
Explanation:
- count is declared inside counter().
- The anonymous function remembers the value of count.
- Every time next() is called, count is incremented.
To know more about closures, refer to this article: Closures