-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadfile.go
40 lines (35 loc) · 936 Bytes
/
readfile.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
package utilities
import (
"os"
"strings"
)
/*
ReadFile reads in the file provided, and splits into a string slice
using the function provided.
This will be the parent function, that can still be called for custom
splits
*/
func ReadFile[T any](file string, split func(f string) T) T {
f, err := os.ReadFile(file)
Check(err)
return split(string(f))
}
func ReadFileLineByLine(file string) []string {
return ReadFile(file, func(f string) []string {
return strings.Split(strings.TrimSpace(f), "\n")
})
}
func ReadFileDoubleLine(file string) []string {
return ReadFile(file, func(f string) []string {
return strings.Split(strings.TrimSpace(f), "\n\n")
})
}
func ReadFileDoubleSingle(file string) [][]string {
return ReadFile(file, func(f string) [][]string {
res := [][]string{}
for _, line := range strings.Split(strings.TrimSpace(f), "\n\n") {
res = append(res, strings.Split(line, "\n"))
}
return res
})
}