Using Anonymous Functions as Callback Functions
Anonymous functions can also be used as callbacks, which means we can pass them as arguments to other functions. This is useful when you want to customize the behavior of a function without creating a named function.
What are Callback Functions?
A callback function is a function that is passed as an argument to another function and is executed after the first function completes its task. This allows the caller to customize the behavior of the function being called, providing more flexibility and modularity. In essence, the function receiving a callback will call the callback function "back" at some point.
Why Use Anonymous Functions as Callbacks?
Anonymous functions work exceptionally well as callback functions because they often represent short, specific behaviors that are only used within a particular context. Using an anonymous function as a callback keeps the code more concise and avoids having to define a separate named function.
Replace anonymous.go
with the following code:
package main
import (
"fmt"
"math"
)
// 'visit' takes a slice and a function. It applies the function to each element in the slice.
func visit(lst []float64, f func(float64)) {
for _, value := range lst {
f(value)
}
}
func main() {
arr := []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}
// Use an anonymous function to sum each element with itself
visit(arr, func(v float64) {
fmt.Printf("Sum:%.0f ", v+v)
})
fmt.Println()
// Use an anonymous function to multiply each element by itself
visit(arr, func(v float64) {
fmt.Printf("Product:%.0f ", v*v)
})
fmt.Println()
// Use an anonymous function to square each element using math.Pow
visit(arr, func(v float64) {
v = math.Pow(v, 2)
fmt.Printf("Square:%.0f ", v)
})
fmt.Println()
}
Run the program:
go run anonymous.go
Expected output:
Sum:2 Sum:4 Sum:6 Sum:8 Sum:10 Sum:12 Sum:14 Sum:16 Sum:18
Product:1 Product:4 Product:9 Product:16 Product:25 Product:36 Product:49 Product:64 Product:81
Square:1 Square:4 Square:9 Square:16 Square:25 Square:36 Square:49 Square:64 Square:81
In this program, we first create a visit
function that takes a slice (lst
) of float64
and a function (f
) of type func(float64)
. The visit
function iterates over the slice and calls the provided function f
for each element. This design pattern enables the visit
function to execute different logic depending on the provided callback function f
.
Inside the main
function, we call visit
three times with different anonymous functions to demonstrate how callbacks provide flexibility.
- The first anonymous function calculates the sum of each element with itself.
- The second anonymous function calculates the product of each element with itself.
- The third anonymous function squares each element using
math.Pow
.
This illustrates how an anonymous function can be passed as a callback and how the visit
function can perform different actions based on the callback function passed as a parameter. This makes your code more reusable and modular.