-
Notifications
You must be signed in to change notification settings - Fork 2
/
func_closure.go
41 lines (39 loc) · 1.01 KB
/
func_closure.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
// possible to create functions inside functions.
// Go supports anonymous function.
// An anonymous function is a function which doesn’t contain any name.
// It is useful when you want to create an inline function.
// In Go language, an anonymous function can form a closure.
// An anonymous function is also known as `function literal`.
/* func(parameter_list) (return_type){
-- code --
-- use return statement if return type is given
-- if return type is not given then do not use
-- return statement.
return
}()
*/
// local function can access local variables inside func.
package main
import "fmt"
// func inside func, returns func.
// defined inline func without name.
func val() func() int{
i := 0
return func() int{
i++
return i
}
}
func main(){
// Assigning an anonymous
// function to a variable
add := func(x, y int)int{
return x+y
}
fmt.Println(add(15,20))
// assigning in a variable
anony := val()
fmt.Println(anony())
fmt.Println(anony())
// val() empty result
}