-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
82 lines (67 loc) · 2.06 KB
/
string.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package fsutil
import (
"bufio"
"io/fs"
"os"
)
// ReadString reads path and returns its content as string.
func ReadString(path string) (string, error) {
data, err := os.ReadFile(path)
if err != nil {
return "", err
}
return string(data), nil
}
// WriteString writes a string content to a file.
//
// Permission perm is optional and defaults to [os.ModePerm].
func WriteString(path, content string, perm ...fs.FileMode) error {
// Append the default permission.
perm = append(perm, os.ModePerm)
return os.WriteFile(path, []byte(content), perm[0])
}
// ReadLines reads a file and returns its content as a slice of strings.
func ReadLines(path string) (lines []string, err error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
// The split function of [bufio.Scanner] defaults to SplitLines.
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return lines, nil
}
// WriteLines writes a slice of string as lines to a file.
//
// WriteLines uses platform-specific linebreak character (LF on Unix, CRLF of Windows).
// Use [WriteLinesWithNewlineChar] if specific newline character is required.
//
// Permission perm is optional and defaults to [os.ModePerm].
func WriteLines(path string, content []string, perm ...fs.FileMode) error {
return WriteLinesWithNewlineChar(path, content, NewLine, perm...)
}
// WriteLinesWithNewlineChar is like [WriteLines], but allows to set newline character used.
func WriteLinesWithNewlineChar(path string, content []string, newline string, perm ...fs.FileMode) error {
// Append the default permission.
perm = append(perm, os.ModePerm)
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, perm[0])
if err != nil {
return err
}
defer file.Close()
writer := bufio.NewWriter(file)
for i, line := range content {
writer.WriteString(line)
// Write a newline character if this is not the last line.
if i+1 < len(content) {
writer.WriteString(NewLine)
}
}
return writer.Flush()
}