arrayutils.go (894B)
1 package main 2 3 // Index returns the first index of the target string t, or -1 if no 4 // match is found 5 func index(terms []string, s string) int { 6 for i, v := range terms { 7 if v == s { 8 return i 9 } 10 } 11 return -1 12 } 13 14 // Include returns true if the target string t is in the slice 15 func Include(terms []string, term string) bool { 16 return index(terms, term) >= 0 17 } 18 19 // Remove returns a []string with all instances of term removed, or 20 // unchanged if the term isn't in the slice 21 func Remove(terms []string, term string) []string { 22 var result []string 23 for _, t := range terms { 24 if t != term { 25 result = append(result, t) 26 } 27 } 28 if result != nil { 29 return result 30 } 31 return []string{} 32 } 33 34 // notAllSame returns true if not every element of a string slice is the same 35 func notAllSame(s []string) bool { 36 for i := 0; i < len(s); i++ { 37 if s[i] != s[0] { 38 return true 39 } 40 } 41 return false 42 }