-
Notifications
You must be signed in to change notification settings - Fork 0
/
loop.go
67 lines (56 loc) · 968 Bytes
/
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
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
// 将十进制的数字转为二进制表示
func convertToBin(n int) string {
if n == 0 {
return "0"
}
res := ""
for ; n > 0; n /= 2 {
lsb := n % 2
res = strconv.Itoa(lsb) + res
}
return res
}
func printFile(filename string) {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
printFileContents(file)
//for i := 1; i <= 100; i++ {
// fmt.Println(i)
//}
// 死循环
//for {
// fmt.Println("ever")
//}
}
func printFileContents(reader io.Reader) {
scanner := bufio.NewScanner(reader)
// 相当于while循环
for scanner.Scan() {
// 循环扫描读取文件 并输出每一行的文件内容
fmt.Println(scanner.Text())
}
}
func main() {
fmt.Println(
convertToBin(5), // 101
convertToBin(13), // 1101
convertToBin(0), // 0
)
printFile("abc.txt")
s := `abbbc"d"
gggg
1222222
pp`
printFileContents(strings.NewReader(s))
}