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 slice, Go String, Go 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!
