Go language provides a special feature known as an anonymous function. An anonymous function is a function which doesn’t contain any name. It is useful when you want to create an inline function. In Go language, an anonymous function can form a closure. An anonymous function is also known as function literal.
Syntax:
func(parameter_list)(return_type){
// code..
// Use return statement if return_type are given
// if return_type is not given, then do not
// use return statement
return
}()
Example:
// Go program to illustrate how // to create an anonymous function package main import "fmt" func main() { // Anonymous function func(){ fmt.Println("Welcome! to GeeksforGeeks") }() } |
Output:
Welcome! to GeeksforGeeks
Important Points:
- In Go language, you are allowed to assign an anonymous function to a variable. When you assign a function to a variable, then the type of the variable is of function type and you can call that variable like a function call as shown in the below example.
Example:
// Go program to illustrate// use of an anonymous functionpackage mainimport"fmt"func main() {// Assigning an anonymous// function to a variablevalue := func(){fmt.Println("Welcome! to GeeksforGeeks")}value()}chevron_rightfilter_noneOutput:
Welcome! to GeeksforGeeks
- You can also pass arguments in the anonymous function.
Example:
// Go program to pass arguments// in the anonymous functionpackage mainimport"fmt"func main() {// Passing arguments in anonymous functionfunc(ele string){fmt.Println(ele)}("GeeksforGeeks")}chevron_rightfilter_noneOutput:
GeeksforGeeks
- You can also pass an anonymous function as an argument into other function.
Example:
// Go program to pass an anonymous// function as an argument into// other functionpackage mainimport"fmt"// Passing anonymous function// as an argumentfunc GFG(i func(p, q string)string){fmt.Println(i ("Geeks","for"))}func main() {value:= func(p, q string) string{returnp + q +"Geeks"}GFG(value)}chevron_rightfilter_noneOutput:
GeeksforGeeks
- You can also return an anonymous function from another function.
Example:
// Go program to illustrate// use of anonymous functionpackage mainimport"fmt"// Returning anonymous functionfunc GFG() func(i, j string) string{myf := func(i, j string)string{returni + j +"GeeksforGeeks"}returnmyf}func main() {value := GFG()fmt.Println(value("Welcome ","to "))}chevron_rightfilter_noneOutput:
Welcome to GeeksforGeeks
Recommended Posts:
- Recursive Anonymous Function in Golang
- Anonymous Structure and Field in Golang
- Function Returning Multiple Values in Go Language
- Constants- Go Language
- Functions in Go Language
- Identifiers in Go Language
- Loops in Go Language
- Select Statement in Go Language
- Go Programming Language (Introduction)
- Loop Control Statements in Go Language
- Difference Between Go and Python Programming Language
- How to get int63 type random number in Go language?
- Golang Tutorial - Learn Go Programming Language
- Pointers to a Function in Go
- Returning Pointer from a Function in Go
- Function Arguments in Golang
- math.Y1() Function in Golang With Examples
- math.Yn() Function in Golang With Examples
- math.NaN() Function in Golang With Examples
- math.Y0() Function in Golang With Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

