← Back to Go Course | Chapter 4: Functions | Lesson 5 of 7

Anonymous Functions

An anonymous function is a mini recipe with no name, written right where you need it, used once and thrown away -- or handed to someone else to use later.
Syntax
go
func(param type) returnType {
    // body
}

varName := func(param type) returnType {
    // body
}

Defining and Calling an Anonymous Function

A function literal omits a name and can be assigned to a variable, which is then called just like any regular function using that variable name followed by parentheses.

Example: Defining and Calling an Anonymous Function

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Short declaration: create square and infer the type
	square := func(n int) int {
		// Send n * n back to the caller
		return n * n
	}
	// Print the values followed by a newline
	fmt.Println(square(7))
}

Immediately Invoked Function Literals

An anonymous function can be called the instant it's defined by appending arguments in parentheses right after its closing brace, useful for scoping a one-off block of logic.

Example: Immediately Invoked Function Literals

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Short declaration: create result and infer the type
	result := func(a, b int) int {
		// Send a + b back to the caller
		return a + b
	}(3, 4)
	// Print the values followed by a newline
	fmt.Println(result)
}

Anonymous Functions as Arguments

Anonymous functions are frequently passed directly as arguments to other functions that expect a callback, avoiding the need to declare and name a separate function used in only one place.

Note: This pattern is common with sort.Slice, which takes an anonymous comparison function.

Example: Anonymous Functions as Arguments

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"sort"
)

// main is where the program starts running
func main() {
	// Short declaration: create nums and infer the type
	nums := []int{5, 2, 8, 1}
	sort.Slice(nums, func(i, j int) bool {
		// Send nums[i] < nums[j] back to the caller
		return nums[i] < nums[j]
	})
	// Print the values followed by a newline
	fmt.Println(nums)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Giving an anonymous function assigned to a variable a redundant name inside the func keyword, which Go doesn't allow (func literals have no name).
  2. Forgetting to add '()' after an anonymous function literal to actually invoke it immediately (an IIFE), leaving it just defined but never called.
  3. Overusing deeply nested anonymous functions where a small named function would be far more readable.
Chapter Summary
  • An anonymous function is a function literal with no name, defined inline where it's used.
  • Anonymous functions can be assigned to a variable and called through it.
  • They can also be invoked immediately after definition by appending () to the literal.
  • They're commonly used as short callback arguments or for goroutines.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.