Rather than tie yourself to only one type (string), you could use the reflect
package as well as interfaces to make it somewhat type indifferent. The following is my reworking of your code:
package main
import "fmt"
import "reflect"
func in_array(val interface{}, array interface{}) (exists bool, index int) {
exists = false
index = -1
switch reflect.TypeOf(array).Kind() {
case reflect.Slice:
s := reflect.ValueOf(array)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
index = i
exists = true
return
}
}
}
return
}
Note that we now import the reflect
package. We also changed the types of val
and array
to interface{}
so that we may pass any type in. We then use the reflect.Typeof()
to glean the reflection reflect.Type
of the value in the array interface{}
. We then glean the type with Kind()
, and use a case to fall into our inner code if its a slice (can add more cases to extend this).
In our inner code, we get the value of the array
argument, and store it in s
. We then iterate over the length of s
, and compare val
to s
at the index i
declared as an interface
with Interface()
and check for truthiness. If its true, we exit with a true and the index.
Running the main function with both a slice of strings and a slice of integers, as follows, works:
func main() {
names := []string{"Mary", "Anna", "Beth", "Johnny", "Beth"}
fmt.Println(in_array("Anna", names))
fmt.Println(in_array("Jon", names))
ints := []int{1, 4, 3, 2, 6}
fmt.Println(in_array(3, ints))
fmt.Println(in_array(95, ints))
}
The above example gets us:
true 1
false -1
true 2
false -1
EDIT: June 2021 - refactored the above code as follows:
func inArray(val interface{}, array interface{}) (index int) {
values := reflect.ValueOf(array)
if reflect.TypeOf(array).Kind() == reflect.Slice || values.Len() > 0 {
for i := 0; i < values.Len(); i++ {
if reflect.DeepEqual(val, values.Index(i).Interface()) {
return i
}
}
}
return -1
}
with the runner function:
func main() {
name := "Mary Anna"
fmt.Println(inArray("Anna", name))
var empty []string
fmt.Println(inArray("Anna", empty))
names := []string{"Mary", "Anna", "Beth", "Johnny", "Beth"}
fmt.Println(inArray("Anna", names))
fmt.Println(inArray("Jon", names))
ints := []int{1, 4, 3, 2, 6}
fmt.Println(inArray(3, ints))
fmt.Println(inArray(95, ints))
}
ok bool
anderr error
are usually the second parameter. See e.g.val, exists := mymap[key]
. \$\endgroup\$