-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
48 lines (39 loc) · 988 Bytes
/
util.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
package libstandard
import "strings"
// Unescape removes backslashes and double-quotes from strings
func Unescape(s string) string {
s = strings.ReplaceAll(s, "\\", "")
s = strings.ReplaceAll(s, "\"", "")
return s
}
// Unique removes all duplicate values from the given slice
func Unique(stringSlice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range stringSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
// FirstOrEmpty returns the first string from the slice.
func FirstOrEmpty(slice []string) string {
if len(slice) > 0 {
return slice[0]
}
return ""
}
// ToMap converts a string-slice to a map[string]string
func ToMap(slice []string) map[string]string {
m := make(map[string]string, 0)
for _, s := range slice {
if len(strings.TrimSpace(s)) == 0 {
continue
}
splitted := strings.Split(s, "=")
m[splitted[0]] = splitted[1]
}
return m
}