-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctional.go
73 lines (61 loc) · 1.04 KB
/
functional.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
69
70
71
72
73
package main
import (
"fmt"
"reflect"
"runtime"
"strconv"
)
func eval(a, b, c int, d string) string {
fmt.Println(
a,
b,
c,
d,
)
return strconv.FormatBool(false)
}
func div(a, b int) (q, r int) {
return a % b, a / b
}
func err(a int) (string, error) {
switch {
case 1 == a:
return "success", nil
case 2 == a:
fmt.Print("fail")
return "success", nil
default:
return "success", fmt.Errorf("unsupported num: %s", "错误")
}
}
func add(a, b int) string {
return strconv.Itoa(a + b)
}
func apply(op func(int, int) string, a, b int) (string) {
fmt.Printf("Calling %s with %d , %d args:",
runtime.FuncForPC(reflect.ValueOf(op).Pointer()).Name(),
a,
b,
)
return op(a, b)
}
func sum(num ... int) int {
s := 0
for i := range num {
s += num[i]
}
return s
}
func main() {
fmt.Println(sum(1, 2, 3, 4, 5))
i := apply(add, 1, 2)
fmt.Println("返回值:" + i)
if res, err := err(34); err != nil {
fmt.Println(err)
} else {
fmt.Println(res)
}
q, _ := div(1, 2)
fmt.Println(q)
fmt.Println(eval(1, 2, 3, "4"))
}