/
file.go
48 lines (37 loc) · 856 Bytes
/
file.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
package utilities
import (
"io/ioutil"
)
const linefeed = '\n'
// ReadLinesFromFile reads the entire file specified, and returns a slice
// of strings
func ReadLinesFromFile(filename string) ([]string, error) {
raw, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var lines []string
var line string
for _, b := range raw {
if b == linefeed {
lines = append(lines, line)
line = ""
continue
}
line += string(b)
}
return lines, nil
}
// ReadIntegersFromFile reads a file and attempts to parse each line as an
// integer, returning the resultant slice of integers.
func ReadIntegersFromFile(filename string) ([]int, error) {
lines, err := ReadLinesFromFile(filename)
if err != nil {
return nil, err
}
nums, err := SliceStringsToInts(lines)
if err != nil {
return nil, err
}
return nums, nil
}