slicesutils.go 877 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package slicesutils
  2. /*
  3. Contains checking if the e string is present in the slice s
  4. */
  5. func Contains(s []string, e string) bool {
  6. for _, a := range s {
  7. if a == e {
  8. return true
  9. }
  10. }
  11. return false
  12. }
  13. /*
  14. Remove removes the entry with the index i from the slice
  15. */
  16. func Remove(s []string, i int) []string {
  17. s[i] = s[len(s)-1]
  18. // We do not need to put s[i] at the end, as it will be discarded anyway
  19. return s[:len(s)-1]
  20. }
  21. /*
  22. Remove removes the e entry from the s slice, if e is not present in the slice, nothing will happen
  23. */
  24. func RemoveString(s []string, e string) []string {
  25. index := Find(s, e)
  26. if index >= 0 {
  27. return Remove(s, index)
  28. }
  29. return s
  30. }
  31. /*
  32. Find finding the index of the e string in the s slice
  33. */
  34. func Find(s []string, e string) int {
  35. for i, n := range s {
  36. if e == n {
  37. return i
  38. }
  39. }
  40. return -1
  41. }