Higher Order Functions in Golang
A Higher-Order function is a function that receives a function as an argument or returns the function as output.
Higher order functions are functions that operate on other functions, either by taking them as arguments or by returning them.
Passing Functions as Arguments to other Functions
Example
package main
import "fmt"
func sum(x, y int) int {
return x + y
}
func partialSum(x int) func(int) int {
return func(y int) int {
return sum(x, y)
}
}
func main() {
partial := partialSum(3)
fmt.Println(partial(7))
}
Output
10
In the program above, the partialSum function returns a sum function that takes two int arguments and returns a int argument.
Returning Functions from other Functions
Example
package main
import "fmt"
func squareSum(x int) func(int) func(int) int {
return func(y int) func(int) int {
return func(z int) int {
return x*x + y*y + z*z
}
}
}
func main() {
// 5*5 + 6*6 + 7*7
fmt.Println(squareSum(5)(6)(7))
}
Output
110
In the program above, the squareSum function signature specifying that function returns two functions and one integer value.
Most Helpful This Week
Creating a Function in Golang
Simple function with return value in Golang
Closures Functions in Golang
The return values of a function can be named in Golang
Golang Passing Address to a Function
What is Function in Golang
Naming Conventions for Golang Functions
User Defined Function Types in Golang
Simple function with parameters in Golang
Anonymous Functions in Golang
Golang Functions Returning Multiple Values