## How to pass a function as an argument using a higher-order function in go?

This tutorial is all about, how to pass a function as a parameter to another function. This is something like magic. Go function can be pass as a parameter to another function and that a function is called a higher-order function.

Example:

``````package main

import "fmt"

func add(x int, y int) int {

return x+y
}

// substraction of two no
func sub(x int, y int) int {

return x-y
}

// pass above two function as argument
func apply(x int, y int, f func(int,int) int) int {

r := f(x,y)
return r
}

func main() {
r2 := apply(3,2, sub)
fmt.Println(r1)
fmt.Println(r2)
}``````

In the above code snippet we have two function, where one will do addition and another will do subtraction. Consider we are building calculator which allows two operation addition and subtraction and based user input we have to perform task and return result.

This feature we can use to have abstraction or hide implementation.

``````// addition of two no
func add(x int, y int) int {

return x+y
}

// substraction of two no
func sub(x int, y int) int {

return x-y
}``````

and the higher function code snippet is below.

``````// pass above two function as argument
func apply(x int, y int, f func(int,int) int) int {

r := f(x,y)
return r
}``````

Here in input parameter, you have to pass two number parameter and one in function itself but here we haven’t mentioned name of that function, we just mentioned about method signature such as whats input and output along with datatype.

Output:-

``````\$ go run funcAsParam.go
5
1``````

Go higher order function support features to return function as a value from function.

Example:

``````package main

import "fmt"

func add(x int, y int) int {

return x+y
}

// substraction of two no
func sub(x int, y int) int {

return x-y
}

// pass above two function as argument
func apply(x int, y int, f func(int,int) int) int {

r := f(x,y)
return r
}
// higher order function to return function
func getAddSub() (func(int, int) int, func(int, int) int) {

add := func(x, y int) int {
return x + y
}

sub := func(x, y int) int {
return x - y
}

}

func applyReturn(x, y int, add func(int, int) int, sub func(int, int) int) (int, int) {

r2 := sub(x, y)

return r1, r2
}

func main() {

x := 3
y := 2

r2 := apply(x,y, sub)
fmt.Println(r1)
fmt.Println(r2)

// return function
fmt.Println("function as a return type")
re1, re2 := applyReturn(x, y, add, sub)

fmt.Printf("%d + %d = %d\n", x, y, re1)
fmt.Printf("%d - %d = %d\n", x, y, re2)
}``````

In this above code snippet, we have defined returning two function as a output param.

``````func getAddSub() (func(int, int) int, func(int, int) int) {

add := func(x, y int) int {
return x + y
}

sub := func(x, y int) int {
return x - y
}

}
``````

The above code snippet returns the two functions add and sub as an output parameter.

Output:

``````\$ go run funcAsParam.go
5
1
function as a return type
3 + 2 = 5
3 - 2 = 1``````

Conclusion:
This is a really powerful feature of creating abstraction. In this tutorial, we learned higher-order functions in go which provide the feature of the pass function as an argument to other functions and also return a function from others function.

Source Code:

``````https://github.com/maheshwarLigade/spring-boot-examples