-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.go
More file actions
51 lines (44 loc) · 835 Bytes
/
utility.go
File metadata and controls
51 lines (44 loc) · 835 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
package main
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
)
func pr(f string, v ...interface{}) {
fmt.Printf(f, v...)
}
func loadList(path string) ([]string, error) {
var list []string
in, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
l := strings.Split(string(in), "\n")
for _, s := range l {
if len(s) > 0 {
list = append(list, s)
}
}
return list, nil
}
func saveString(s, path string) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer func() {
err = f.Close()
if err != nil {
pr("Error closing %s: %s", path, err.Error())
}
}()
_, err = f.WriteString(s)
return err
}
// mangleName turns a file name into alphanumerics only.
func mangleName(name string) string {
r, _ := regexp.Compile("[^0-9a-zA-Z]+")
return r.ReplaceAllString(name, "")
}