-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.go
35 lines (29 loc) · 706 Bytes
/
tools.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
package favdb
import "reflect"
func TruncateString(s string, maxLength int) string {
if len(s) <= maxLength {
return s
}
return s[:maxLength]
}
// InArray: Check if string item is in array
func InArray(val interface{}, array interface{}) (exists bool, index int) {
exists = false
index = -1
switch reflect.TypeOf(array).Kind() {
case reflect.Slice:
s := reflect.ValueOf(array)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
index = i
exists = true
return
}
}
}
return
}
// RemoveStringItem: Remove string item from slice
func RemoveStringItem(slice []string, s int) []string {
return append(slice[:s], slice[s+1:]...)
}