-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.go
49 lines (42 loc) · 771 Bytes
/
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
48
49
package main
import (
"fmt"
"reflect"
"runtime"
"math"
)
func eval(a, b int, op string) int {
switch op {
case "+":
return a + b
case "-":
return a - b
case "*":
return a * b
case "/":
return a / b
default:
panic("unsupported operation:" + op)
}
}
func div(a,b int) (q, r int) {
// return a / b, a % b
q = a /b
r = a % b
return
}
func applv(op func(int, int) int, a, b int) int {
p := reflect.ValueOf(op).Pointer()
opName := runtime.FuncForPC(p).Name()
fmt.Printf("Calling function %s with args " + "(%d , %d)", opName, a, b)
return op(a, b)
}
func pow (a, b int) int {
return int(math.Pow(float64(a), float64(b)))
}
func main() {
fmt.Println(eval(3, 4, "*"))
q, r := div(13, 3)
fmt.Println(q, r)
fmt.Println(applv(pow, 3, 4))
}