-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringslice.go
46 lines (39 loc) · 861 Bytes
/
stringslice.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
package stringslice
// Contains returns whether the given slice contains the given item
func Contains(list []string, item string) bool {
for _, v := range list {
if v == item {
return true
}
}
return false
}
// Remove the given item from the given list if exists
func Remove(list []string, item string) []string {
for i, v := range list {
if v == item {
list = append(list[:i], list[i+1:]...)
}
}
return list
}
// Equals checks whether two string slices are equal, i.e. have the same length and contain the same elements
func Equals(list1, list2 []string) bool {
if len(list1) != len(list2) {
return false
}
if list1 == nil || list2 == nil {
return false
}
for _, s := range list1 {
if !Contains(list2, s) {
return false
}
}
for _, s := range list2 {
if !Contains(list1, s) {
return false
}
}
return true
}