-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_ret_func.go
47 lines (41 loc) · 1007 Bytes
/
func_ret_func.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/************************************************
* Function Returning Another Function *
************************************************
*
* We see functions x and y which return another
* lambda function : func(b int) int
*
* func x() (func(b int) int)
* func y(a int) (func(b int) int)
*
* x() takes no parameters
* y() takes an int as parameter
*
************************************************
* Outputs : *
************************************************
*
* Call x() for 3 gives : 5
* Call y(2) for 3 gives : 5
*
*/
package main
import "fmt"
func main() {
// Make a func x(), give it a name p, and call it.
p := x()
fmt.Printf("Call x() for 3 gives : %v\n", p(3))
// Make a special func y(a int), a gets 3.
p2 := y(2)
fmt.Printf("Call y(2) for 3 gives : %v\n", p2(3))
}
func x() func(b int) int {
return func(b int) int {
return b + 2
}
}
func y(a int) func(b int) int {
return func(b int) int {
return a + b
}
}