-
Notifications
You must be signed in to change notification settings - Fork 44
/
slice.go
47 lines (44 loc) · 1.02 KB
/
slice.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
package controller
// Some basic slice helpers that are lacking from Go stdlib…
//
// Note that we only defines the helpers for `[]string` because:
// - we only need it for `[]string` for now
// - Go doesn't support generics anyway…
// Remove all the occurrences `target` from `slice` (if present).
//
// Arguments
//
// slice: the slice to process
// target: the value to remove
//
// Returns
//
// A slice without `target`.
func SliceRemoveValue(slice []string, target string) []string {
newSlice := slice[:0]
for _, value := range slice {
if value != target {
newSlice = append(newSlice, value)
}
}
return newSlice
}
// Append `value` to `slice (if not already present).
//
// Arguments
//
// slice: the slice to process
// target: the value to add.
//
// Returns
//
// A slice with `target` in it.
func SliceAppendUnique(slice []string, newValue string) []string {
for _, value := range slice {
// value already present: nothing to do.
if value == newValue {
return slice
}
}
return append(slice, newValue)
}