-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
44 lines (37 loc) · 837 Bytes
/
slice.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
package armory
import (
"errors"
"reflect"
)
type slice struct{}
// Slice Slice
var Slice *slice
/*
func (arr Slice) IndexOf(ele interface{}) int {
r := -1
for idx, val := range arr {
if ele == val {
r = idx
}
}
return r
}
*/
// IndexOf IndexOf
func (s *slice) IndexOf(params ...interface{}) (int, error) {
arr := reflect.ValueOf(params[0])
v := reflect.ValueOf(params[1])
var t = reflect.TypeOf(params[0]).Kind()
if t != reflect.Slice && t != reflect.Array {
return -1, errors.New("Type Error! First argument must be an array or a slice")
}
for i := 0; i < arr.Len(); i++ {
if arr.Index(i).Kind() != v.Kind() {
return -1, errors.New("Type Error! Second argument must matched any elements in first argument")
}
if arr.Index(i).Interface() == v.Interface() {
return i, nil
}
}
return -1, nil
}