...

/

Functions That Return Other Functions

Functions That Return Other Functions

Let’s learn about functions that can return other functions.

We'll cover the following...

Apart from accepting functions as arguments, functions can also return anonymous functions, which can be handy when the returned function is not always the same but depends on the function's input or other external parameters.

Coding example

This is illustrated in returnFunction.go:

Press + to interact
package main
import "fmt"
func funRet(i int) func(int) int {
if i < 0 {
return func(k int) int {
k = -k
return k + k
}
}
return func(k int) int {
return k * k
}
}

The signature of funRet() declares that the ...