Anonymous Functions in Golang

Learn via video courses
Topics Covered

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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.

Syntax

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Examples

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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

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

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.