Table of contents
1.
Introduction
2.
Anonymous Function in Go
3.
Working of Anonymous Function
4.
Functions of Anonymous Function
4.1.
Anonymous Function with Parameters
4.2.
Return Value from Anonymous Function
4.2.1.
Example 1
4.2.2.
Example 2
4.3.
Anonymous Function as Arguments to Other Functions
4.4.
Return an Anonymous Function from a Function
5.
Frequently Asked Questions
5.1.
What are the uses of Anonymous functions?
5.2.
What are the advantages of Anonymous functions?
5.3.
Is it feasible to use a function inside a function in the Go language?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Go Anonymous Function

Author Sagar Mishra
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Go, commonly known as Golang, is an open-source, statically typed, compiled computer programming language created by Google. It is designed to be easy, readable, high-performing, and efficient. 

In this article, we will understand how Go Anonymous Functions work with the help of some examples.

Anonymous Function in Go

An anonymous function is a function in Go that can be created without a function name. Like standard functions, anonymous functions can take arguments as input and return results as output.

Functionality that does not need to be named and perhaps for temporary use is known as anonymous functions.

Example

func () {
  fmt.Println("Example of a Function without any name")
}

The above function, which prints "Example of a Function without any name," is valid. It works exactly like a regular function in Go.

Working of Anonymous Function

You might be asking how we can invoke the function since an anonymous function has no name.

The anonymous function is typically assigned to a variable, which is then used to invoke the function. For example,

package main
import "fmt"

func main() {

  // anonymous function
  var welcome= func() {
    fmt.Println("Hello, Welcome to Coding Ninjas!")
  }

  // function call
  welcome()
}

 

Output

Functions of Anonymous Function

Some of the main  functions of Anonymous Functions are listed below:

Anonymous Function with Parameters

An anonymous function can accept function parameters like a regular function.

For example

// Program to pass arguments in an anonymous function

package main
import "fmt"

func main() {

  // anonymous function
  var sub = func(n1, n2 int) {
    sub := n1 - n2
    fmt.Println("Difference is", sub)
  } 

// function call
sub(5, 3)

}

 

Output

In this example, the anonymous function took two integers as an argument that is n1 and n2. And, since we have assigned the anonymous function in a variable named "sub," we call the function using the variable name.

Return Value from Anonymous Function

We can also use the “return” statement in an anonymous function as a regular function. Let’s understand with some examples:

Example 1

// Program to return value from an anonymous function
package main
import "fmt"

func main() {

  // anonymous function
  var sub = func(n1, n2 int) int {
    sub := n1 - n2
  
    return sub
  } 

// function call
result := sub(5, 3)

fmt.Println("Difference is", result)

}

 

Output

In the above example, the "sub" variable has been given the anonymous function func(n1,n2 int) int. The function calculates the difference between n1 and n2 and returns it as output.

Example 2

// Program to return the area of a square

package main
import "fmt"

func main() {

  // anonymous function
  area := func(side int) int {
    return side * side
  } 

  // function call using variable name
  fmt.Println("The area of square is", area(4))

}

 

Output

We defined an anonymous function and assigned it to the variable area in the program shown above. Side *  Side (4 * 4) returns the square's area to the area variable.

Anonymous Function as Arguments to Other Functions

In Go, anonymous functions can also be passed as arguments to other functions. The variable containing the anonymous function is then passed in such a situation. 

For example,

package main
import "fmt"

var sub = 0

// A function to calculate square of numbers
func areaOfSquare(side int) int {
  square := side * side
  return square
}

func main() {

  // anonymous function that returns difference of numbers
  sub := func(number1 int, number2 int) int {
    return number1 - number2
}

  // function call
  result := areaOfSquare(sub(7, 3))
  fmt.Println("Resultant Area of Square is", result)
}

 

Output

In the above-mentioned example, we have created two functions. One regular function named areaOfSquare() accepts an integer value as an argument. And the second function is an anonymous function that returns the difference between two numbers.

In the main function, we have passed the anonymous function as the argument of the regular function areaOfSquare().

Return an Anonymous Function from a Function

Go also allows us to return an anonymous function. To do that, we must create and return an anonymous function inside a regular function. 

For example,

// Program to return an anonymous function 

package main
import "fmt"

// function that returns an anonymous function
func displayNumber() func() int {

  num := 20
  return func() int {
    num++
    return num
  }
}

func main() {

  a := displayNumber()

  fmt.Println(a())

}

 

Output

In this case, the func() indicates that the anonymous function returns a function, but the int indicates that the displayNumber() returns an integer. The displayNumber() is a regular function, as you can see.

Frequently Asked Questions

What are the uses of Anonymous functions?

A function without a name is referred to as an Anonymous Function. It helps to create an inline function. An anonymous function can also be helpful while creating closure in the Go language.

What are the advantages of Anonymous functions?

An anonymous function's advantage is that it does not need to be stored in a separate file. Since calculations are sometimes reasonably simple, this can substantially simplify programs. Additionally, anonymous functions can reduce the number of code files necessary for a program.

Is it feasible to use a function inside a function in the Go language?

Yes, we can use a function inside a function.

Example

func xyz() func() int{ …}

Conclusion

This article extensively discussed the topic of Go Anonymous Function in detail. We started with an introduction and discussed the working of Anonymous Function along with its different types of functions.

We hope that this blog has helped you enhance your knowledge of the Go Anonymous Function. If you want to learn more, check out our other articles on the topics like Go sliceGo StringGo Select, and many more on our platform Coding Ninjas Studio.

For peeps out there who want to learn more about Data Structures, Algorithms, Power programming languages, JavaScript, interview questions, or any other upskilling, please refer to our guided paths on Coding Ninjas Studio. Enroll in our courses, go for mock tests, solve available problems, and interview puzzles. Also, you can focus on interview stuff- interview experiences and an interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass