The Wayback Machine - https://web.archive.org/web/20240606092652/https://www.geeksforgeeks.org/anonymous-function-in-go-language/
Open In App

Anonymous function in Go Language

Last Updated : 22 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 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
  • You can also pass arguments in the anonymous function.

    Example:




    // 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")
        
    }

    
    

    Output:

    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 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
  • You can also return an anonymous function from another function.

    Example:




    // 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 "))
    }

    
    

    Output:

    Welcome to GeeksforGeeks


Previous Article
Next Article

Similar Reads

Recursive Anonymous Function in Golang
Recursion is a process in which a function calls itself implicitly or explicitly and the corresponding function is called recursive function. Go language supports special feature called anonymous function. It is a function that doesn’t contain any name. It is used to create an inline function. Recursive functions can be declared & defined as an
2 min read
Anonymous Structure and Field in Golang
A structure or struct in Golang is a user-defined type, which allows us to create a group of elements of different types into a single unit. Any real-world entity which has some set of properties or fields can be represented as a struct. Anonymous Structure In Go language, you are allowed to create an anonymous structure. An anonymous structure is
3 min read
Function Returning Multiple Values in Go Language
In Go language, you are allowed to return multiple values from a function, using the return statement. Or in other words, in function, a single return statement can return multiple values. The type of the return values is similar to the type of the parameter defined in the parameter list. Syntax: func function_name(parameter_list)(return_type_list)
3 min read
Select Statement in Go Language
In Go language, the select statement is just like switch statement, but in the select statement, case statement refers to communication, i.e. sent or receive operation on the channel. Syntax: select{ case SendOrReceive1: // Statement case SendOrReceive2: // Statement case SendOrReceive3: // Statement ....... default: // Statement Important points:
4 min read
Loops in Go Language
Go language contains only a single loop that is for-loop. A for loop is a repetition control structure that allows us to write a loop that is executed a specific number of times. In Go language, this for loop can be used in the different forms and the forms are: 1. As simple for loop It is similar that we use in other programming languages like C,
5 min read
Loop Control Statements in Go Language
Loop control statements in the Go language are used to change the execution of the program. When the execution of the given loop left its scope, then the objects that are created within the scope are also demolished. The Go language supports 3 types of loop control statements: Break Goto Continue Break Statement The break statement is used to termi
3 min read
Functions in Go Language
Functions are generally the block of codes or statements in a program that gives the user the ability to reuse the same code which ultimately saves the excessive use of memory, acts as a time saver and more importantly, provides better readability of the code. So basically, a function is a collection of statements that perform some specific task an
4 min read
Difference Between Go and Python Programming Language
Golang is a procedural programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language. Programs are assembled by using packages, for efficient management of dependencies. This language also supports environment adopting patterns alike to dynamic la
2 min read
How to get int63 type random number in Go language?
Go language provides inbuilt support for generating random numbers of the specified type with the help of a math/rand package. This package implements pseudo-random number generators. These random numbers are generated by a source and this source produces a deterministic sequence of values every time when the program run. And if you want to random
2 min read
Auto Format Go Programming Language Source Code with gofmt
Formatting of source code adds an important feature to the quality and readability of the code. In Golang, we have built-in packages and commands to auto-format code in the source file as per the standards and best practices. We can use the gofmt command in the CLI to autoformat a Golang source file. We can autoformat the go code with gofmt package
4 min read