Gaaker Go

Golang Function as return values

Functions are value types, so they can be passed as return values.

The following is a complete code example.

package main

import (
	"fmt"
)

func plus(a int, b int) func() {
    c := a + b
    return func ()  {
		fmt.Println("a + b = ", c)
	}
}

func main() {
	plus(1,1)()

	f := plus(3,3)
	f()
}

Below is the program output:

a + b =  2
a + b =  6
< Function as return values Function as return values >