contains.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package arrays
  2. import (
  3. "reflect"
  4. )
  5. // Contains Returns the index position of the val in array
  6. func Contains(array interface{}, val interface{}) (index int) {
  7. index = -1
  8. switch reflect.TypeOf(array).Kind() {
  9. case reflect.Slice: {
  10. s := reflect.ValueOf(array)
  11. for i := 0; i < s.Len(); i++ {
  12. if reflect.DeepEqual(val, s.Index(i).Interface()) {
  13. index = i
  14. return
  15. }
  16. }
  17. }
  18. }
  19. return
  20. }
  21. // ContainsString Returns the index position of the string val in array
  22. func ContainsString(array []string, val string) (index int) {
  23. index = -1
  24. for i := 0; i < len(array); i++ {
  25. if array[i] == val {
  26. index = i
  27. return
  28. }
  29. }
  30. return
  31. }
  32. // ContainsInt Returns the index position of the int64 val in array
  33. func ContainsInt(array []int64, val int64) (index int) {
  34. index = -1
  35. for i := 0; i < len(array); i++ {
  36. if array[i] == val {
  37. index = i
  38. return
  39. }
  40. }
  41. return
  42. }
  43. // ContainsUint Returns the index position of the uint64 val in array
  44. func ContainsUint(array []uint64, val uint64) (index int) {
  45. index = -1
  46. for i := 0; i < len(array); i++ {
  47. if array[i] == val {
  48. index = i
  49. return
  50. }
  51. }
  52. return
  53. }
  54. // ContainsBool Returns the index position of the bool val in array
  55. func ContainsBool(array []bool, val bool) (index int) {
  56. index = -1
  57. for i := 0; i < len(array); i++ {
  58. if array[i] == val {
  59. index = i
  60. return
  61. }
  62. }
  63. return
  64. }
  65. // ContainsFloat Returns the index position of the float64 val in array
  66. func ContainsFloat(array []float64, val float64) (index int) {
  67. index = -1
  68. for i := 0; i < len(array); i++ {
  69. if array[i] == val {
  70. index = i
  71. return
  72. }
  73. }
  74. return
  75. }
  76. // ContainsComplex Returns the index position of the complex128 val in array
  77. func ContainsComplex(array []complex128, val complex128) (index int) {
  78. index = -1
  79. for i := 0; i < len(array); i++ {
  80. if array[i] == val {
  81. index = i
  82. return
  83. }
  84. }
  85. return
  86. }