Anonymous Functions
In this page:
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
// 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))
}
Login to try C/C++/Java/PHP code in the editor
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
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.
Example: Anonymous Functions as Arguments
// 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)
}
Login to try C/C++/Java/PHP code in the editor
- 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).
- Forgetting to add '()' after an anonymous function literal to actually invoke it immediately (an IIFE), leaving it just defined but never called.
- Overusing deeply nested anonymous functions where a small named function would be far more readable.
- 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: