Open In App

Anonymous function in Go Language

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:


Article Tags :