-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader.go
66 lines (55 loc) · 1.14 KB
/
reader.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
package file
import (
"bufio"
"io/ioutil"
"strconv"
"strings"
)
func ToBytes(filePath string) ([]byte, error) {
return ioutil.ReadFile(filePath)
}
func ToString(filePath string) (string, error) {
b, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
return string(b), nil
}
func ToTrimString(filePath string) (string, error) {
str, err := ToString(filePath)
if err != nil {
return "", err
}
return strings.TrimSpace(str), nil
}
func ToUint64(filePath string) (uint64, error) {
content, err := ToTrimString(filePath)
if err != nil {
return 0, err
}
var ret uint64
if ret, err = strconv.ParseUint(content, 10, 64); err != nil {
return 0, err
}
return ret, nil
}
func ToInt64(filePath string) (int64, error) {
content, err := ToTrimString(filePath)
if err != nil {
return 0, err
}
var ret int64
if ret, err = strconv.ParseInt(content, 10, 64); err != nil {
return 0, err
}
return ret, nil
}
func ReadLine(r *bufio.Reader) ([]byte, error) {
line, isPrefix, err := r.ReadLine()
for isPrefix && err == nil {
var bs []byte
bs, isPrefix, err = r.ReadLine()
line = append(line, bs...)
}
return line, err
}