Skip to content
Related Articles

Related Articles

Anonymous function in Go Language
  • Last Updated : 22 May, 2020

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how
// to create an anonymous function
package main
  
import "fmt"
  
func main() {
      
    // Anonymous function
   func(){
  
      fmt.Println("Welcome! to GeeksforGeeks")
  }()
    
}

chevron_right


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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Go program to illustrate
    // use of an anonymous function
    package main
      
    import "fmt"
      
    func main() {
          
        // Assigning an anonymous 
       // function to a variable
       value := func(){
          fmt.Println("Welcome! to GeeksforGeeks")
      }
      value()
        
    }

    chevron_right

    
    

    Output:

    Welcome! to GeeksforGeeks
  • You can also pass arguments in the anonymous function.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Go program to pass arguments 
    // in the anonymous function
    package main
      
    import "fmt"
      
    func main() {
          
        // Passing arguments in anonymous function
      func(ele string){
          fmt.Println(ele)
      }("GeeksforGeeks")
        
    }

    chevron_right

    
    

    Output:

    GeeksforGeeks
  • You can also pass an anonymous function as an argument into other function.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Go program to pass an anonymous 
    // function as an argument into 
    // other 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)
    }

    chevron_right

    
    

    Output:

    GeeksforGeeks
  • You can also return an anonymous function from another function.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Go program to illustrate
    // use of anonymous 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 "))
    }

    chevron_right

    
    

    Output:

    Welcome to GeeksforGeeks
My Personal Notes arrow_drop_up
Recommended Articles
Page :