-
-
Notifications
You must be signed in to change notification settings - Fork 451
/
in_array.go
executable file
·61 lines (51 loc) · 2.13 KB
/
in_array.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"fmt"
"reflect"
)
// InArray will search element inside array with any type.
// Will return boolean and index for matched element.
// True and index more than 0 if element is exist.
// needle is element to search, haystack is slice of value to be search.
func InArray(needle interface{}, haystack interface{}) (exists bool, index int) {
exists = false
index = -1
switch reflect.TypeOf(haystack).Kind() {
case reflect.Slice:
s := reflect.ValueOf(haystack)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(needle, s.Index(i).Interface()) == true {
index = i
exists = true
return
}
}
}
return
}
func main() {
// Array of string
arrStrings := []string{"foo", "bar", "baz"}
searchString := "bar"
stringExist, stringIndex := InArray(searchString, arrStrings)
fmt.Printf("The '%s' is %v inside arrays %v with index: %d\n", searchString, stringExist, arrStrings, stringIndex)
// Array of int64 (or you can use int or int32 too)
arrInt64 := []int64{2016, 2017, 2018, 2019}
searchInt64 := int64(2016)
int64Exist, int64Index := InArray(searchInt64, arrInt64)
fmt.Printf("The '%d' is %v inside arrays %v with index: %d\n", searchInt64, int64Exist, arrInt64, int64Index)
// Example for false searching
int64NotExist := int64(2000)
isElementExist, foundElementIndex := InArray(int64NotExist, arrInt64)
fmt.Printf("The '%d' is %v inside arrays %v with index: %d\n", int64NotExist, isElementExist, arrInt64, foundElementIndex)
// False searching with different type
// Search string inside array of int64
searchStringInt64 := "2018"
isStringExistInInt64Arr, stringIndexIn64ArrElement := InArray(searchStringInt64, arrInt64)
fmt.Printf("The '%s' (%v) is %v inside arrays %v with index: %d\n", searchStringInt64, reflect.TypeOf(searchStringInt64).Name(), isStringExistInInt64Arr, arrInt64, stringIndexIn64ArrElement)
// Or, you can also using array of interface
arrInterface := []interface{}{"username", 123, int64(10), false}
searchElement := false
ok, index := InArray(searchElement, arrInterface)
fmt.Printf("The '%v' is %v inside arrays %v with index: %d\n", searchElement, ok, arrInterface, index)
}