-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
68 lines (56 loc) · 1.3 KB
/
function.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import . "fmt"
import "strings"
func main() {
Println("addition of 2, 3 is", add(2, 3))
v1, v2 := mulReturn(10)
Println("multiple return", v1, v2)
// closure
num3 := 0
sub := func () int {
num3 += 10
return num3
}
Println(sub())
Println(sub())
// defer prevent executing func. After execution end of caller method
// defer method will be called
defer printOne()
printTwo()
Println("end of main")
foo()
Println("-----start of function with variable number of inputs-------")
rFunc := funcFactory("This", "is", "prefix")
Println(rFunc("This", "is", "suffix"))
Println(rFunc("This", "is", "suffix and with extended value"))
Println("-----end of function with variable number of inputs-------")
}
// normalt function
func add(n, m int) int {
return n + m
}
// function with multiple return
func mulReturn(value int) (int, int) {
return value + 1, value - 1
}
// method defering
func printOne(){
Println(1)
}
func printTwo(){
Println(2)
}
// defer panic and recover
func foo(){
defer func () {
err := recover()
Println("panic has been occured", err)
Println("recovering")
}()
panic("I am paniccing")
}
func funcFactory(prefix... string) (func(postfix... string) string){
return func(suffix... string) string{
return strings.Join(append(prefix, suffix...), " ")
}
}