-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop.go
88 lines (86 loc) · 1.42 KB
/
loop.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"bufio"
"fmt"
"math"
"os"
"reflect"
"runtime"
"strconv"
)
func convertToBin(n int) string {
result := ""
if n > 0 {
for ; n > 0; n /= 2 {
lsb := n % 2
result = strconv.Itoa(lsb) + result
}
return result
} else if n == 0 {
return "0"
} else {
//panic(err)
return "error"
}
}
func printFile(filename string) {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}
func forever() {
for {
fmt.Println("loop")
}
}
func choose(n string) {
if n == "/" {
q, r := div(2, 3)
fmt.Println(q, r)
}
}
func div(n, m int) (q, r int) {
q = n / m
r = n % m
return
}
func apply(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\n", opName, a, b)
return op(a, b)
}
func pow(a, b int) int {
return int(math.Pow(float64(a), float64(b)))
}
func sum(nums ...int) int {
sum := 0
for _, n := range nums {
sum += n
}
return sum
}
func swap(a *int, b *int) {
*a, *b = *b, *a
}
func main() {
fmt.Println(
convertToBin(5), //101
convertToBin(13), //1011-->1101
convertToBin(0),
convertToBin(-1),
)
printFile("test.txt")
//forever()
choose("/")
fmt.Printf("result is %d\n", apply(pow, 2, 3))
fmt.Println(sum(1, 2, 3, 4, 5))
a, b := 3, 4
swap(&a, &b)
fmt.Println(a, b)
}