Anonymous Functions in Golang
Overview
An Anonymous function in Golang refers to a function that does not have a name. These types of functions are also called function literals or lambda functions in some programming languages. In this article, we will learn more about anonymous functions in Golang.
Introduction
An Anonymous function is a function that does not have a name. These functions are used when you want to write your logic inline. An anonymous function is not accessible after its initial creation, it can only be accessed by a variable it is stored in as a function as a value. An anonymous function can also have multiple arguments and return values.
Golang Anonymous Function
Golang allows you to create in-line or anonymous functions for short-term use. This function can have input parameters and return values just like any other function.
We can create an Anonymous function using the below syntax.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Syntax
Examples
Normal Anonymous Function
The below code shows a basic anonymous function declaration that prints a string to the console.
Output:
Normal anonymous function example : playground link
Turn Learning into Career Growth
Assigning the Anonymous Function to a Variable
Golang anonymous functions can be assigned to a variable and then it can be invoked using the same variable. The below examples show how we can assign a function to a variable and invoke it.
Output:
Assigning the anonymous function to a variable example: playground link
Pass Arguments in the Anonymous Function
Golang supports passing arguments to anonymous functions. The below code shows how we can pass a string to an anonymous function and print the concatenated string to the console.
Output:
Pass arguments in the anonymous function example: playground link
Return Value from Anonymous Function
Just like we can have input arguments for anonymous functions we can also return values from those. The below example illustrates how we can return values from anonymous functions.
Output:
Return Value From Anonymous Function example: playground link
Pass an Anonymous Function as an Argument into Other Function
Golang supports passing anonymous functions as other functions' arguments. we need to add the argument details in the function signature as shown in the example.
Output:
Pass an anonymous function as an argument into another function example: playground link
Return an Anonymous Function from Another Function
We can return an anonymous function from another function. This can be achieved by returning a function shown in the below example.
Output:
Return an anonymous function from another function example : playground link
Conclusion
- Understood the concept of Anonymous functions in Golang.
- Implemented examples showcasing different use cases for anonymous functions.