Golang Tutorial
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays & Slices
String
Pointers
Interfaces
Concurrency
Anonymous functions in Go (often called "lambda functions" in other languages) are functions without a name. They can be used inline and you don't need to define a standard function to use them.
Here are the steps to create and use an anonymous function in Go:
This example demonstrates a basic anonymous function in Go:
package main import "fmt" func main() { func() { fmt.Println("This is an anonymous function.") }() }
In this example, the function is defined and immediately invoked. The ()
at the end of the function definition is used to invoke the function.
You can also pass parameters to an anonymous function:
package main import "fmt" func main() { func(name string) { fmt.Println("Hello,", name) }("John") }
You can assign an anonymous function to a variable and then call it:
package main import "fmt" func main() { greet := func(name string) { fmt.Println("Hello,", name) } greet("Alice") greet("Bob") }
Functions can also return anonymous functions:
package main import "fmt" func multiplier(factor int) func(int) int { return func(num int) int { return num * factor } } func main() { double := multiplier(2) fmt.Println(double(5)) // Outputs: 10 triple := multiplier(3) fmt.Println(triple(5)) // Outputs: 15 }
In the above example, the multiplier
function returns an anonymous function which, when invoked, multiplies its argument by the factor specified when creating the function.
Anonymous functions can access and modify variables from the outer function. Such functions are called closures:
package main import "fmt" func counter() func() int { count := 0 return func() int { count++ return count } } func main() { next := counter() fmt.Println(next()) // Outputs: 1 fmt.Println(next()) // Outputs: 2 fmt.Println(next()) // Outputs: 3 }
In this example, the counter
function returns an anonymous function that serves as a closure over the count
variable. Each time the returned function is called, the count increases.
Anonymous functions in Go are quite versatile and can be utilized in various ways: as inline functions, closures, or functional arguments. They are especially useful when you want a short-lived function, for instance, as a one-off for a goroutine or when using functions like sort.Slice
with custom comparators.
Closure in Golang:
func closureExample() func() int { i := 0 return func() int { i++ return i } }
Golang Anonymous Function Examples:
func main() { add := func(a, b int) int { return a + b } result := add(3, 5) fmt.Println(result) // Output: 8 }
Function Literals in Go:
func main() { result := func(a, b int) int { return a * b }(3, 4) fmt.Println(result) // Output: 12 }
Golang Anonymous Function vs Named Function:
// Named function func add(a, b int) int { return a + b } // Anonymous function addAnonymous := func(a, b int) int { return a + b }
Anonymous Functions and Variables in Golang:
func main() { x := 10 increment := func() { x++ } increment() fmt.Println(x) // Output: 11 }
Using Anonymous Functions in Golang Callbacks:
func process(nums []int, callback func(int) int) { for i, v := range nums { nums[i] = callback(v) } }
Golang Higher-Order Functions with Anonymous Functions:
func multiplyBy(factor int) func(int) int { return func(x int) int { return x * factor } }
Capturing Variables in Golang Closures:
func closureExample() func() int { i := 0 return func() int { i++ return i } }