-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_utils.go
More file actions
57 lines (46 loc) · 943 Bytes
/
file_utils.go
File metadata and controls
57 lines (46 loc) · 943 Bytes
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
package main
import (
"bufio"
"os"
"strings"
)
func readlines(path string) (<-chan string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(file)
if err := scanner.Err(); err != nil {
return nil, err
}
chanel := make(chan string)
go func() {
for scanner.Scan() {
chanel <- scanner.Text()
}
close(chanel)
}()
return chanel, nil
}
func LoadFileContentAsString(fileName string) string {
reader, readError := readlines(fileName)
if readError != nil {
panic("cannot load file")
}
builder := strings.Builder{}
for line := range reader {
builder.WriteString(line)
}
return builder.String()
}
func LoadFileContentAsStringsArray(fileName string) []string {
reader, readError := readlines(fileName)
if readError != nil {
panic("cannot load file")
}
result := []string{}
for line := range reader {
result = append(result, line)
}
return result
}