|
@@ -7,20 +7,19 @@ import (
|
7
|
7
|
"unicode/utf8"
|
8
|
8
|
)
|
9
|
9
|
|
10
|
|
-// ReadLines reads all lines from the given path and returns them in a slice of strings.
|
|
10
|
+// ReadFileAsInts reads all lines from the given path and returns them in a slice of ints.
|
11
|
11
|
// If an error occurs, the function will panic.
|
12
|
|
-func ReadLines(path string) []string {
|
13
|
|
- return readWithScanner(path, bufio.ScanLines)
|
|
12
|
+func ReadFileAsInts(path string) []int {
|
|
13
|
+ return readIntsWithScanner(path, bufio.ScanLines)
|
14
|
14
|
}
|
15
|
15
|
|
16
|
|
-// ReadCommas reads all data from the given path and returns a string slice
|
17
|
|
-// containing comma-delimited parts. If a
|
18
|
|
-// If an error occurs, the function will panic.
|
19
|
|
-func ReadCommas(path string) []string {
|
20
|
|
- return readWithScanner(path, scanByCommas)
|
|
16
|
+// ReadCsvAsInts reads all data from the given path and returns an int slice
|
|
17
|
+// containing comma-delimited parts. If an error occurs, the function will panic.
|
|
18
|
+func ReadCsvAsInts(path string) []int {
|
|
19
|
+ return readIntsWithScanner(path, scanByCommas)
|
21
|
20
|
}
|
22
|
21
|
|
23
|
|
-func readWithScanner(path string, splitFunc bufio.SplitFunc) []string {
|
|
22
|
+func readIntsWithScanner(path string, splitFunc bufio.SplitFunc) []int {
|
24
|
23
|
file, err := os.Open(path)
|
25
|
24
|
if err != nil {
|
26
|
25
|
panic(err)
|
|
@@ -29,11 +28,11 @@ func readWithScanner(path string, splitFunc bufio.SplitFunc) []string {
|
29
|
28
|
_ = file.Close()
|
30
|
29
|
}()
|
31
|
30
|
|
32
|
|
- var parts []string
|
|
31
|
+ var parts []int
|
33
|
32
|
scanner := bufio.NewScanner(file)
|
34
|
33
|
scanner.Split(splitFunc)
|
35
|
34
|
for scanner.Scan() {
|
36
|
|
- parts = append(parts, scanner.Text())
|
|
35
|
+ parts = append(parts, MustAtoi(scanner.Text()))
|
37
|
36
|
}
|
38
|
37
|
|
39
|
38
|
if scanner.Err() != nil {
|