2021-09-28 11:47:19 +08:00
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"reflect"
|
|
|
|
|
)
|
|
|
|
|
|
2022-03-17 12:11:38 +08:00
|
|
|
|
// [1,2,3, 4, 5,6,7]
|
|
|
|
|
// 7
|
|
|
|
|
// mid: 3
|
2021-09-28 11:47:19 +08:00
|
|
|
|
func InArray(search, needle interface{}) bool {
|
|
|
|
|
val := reflect.ValueOf(needle)
|
|
|
|
|
|
|
|
|
|
kind := val.Kind()
|
|
|
|
|
|
|
|
|
|
if kind == reflect.Slice || kind == reflect.Array {
|
2022-03-17 12:11:38 +08:00
|
|
|
|
_length := val.Len()
|
|
|
|
|
|
|
|
|
|
if _length <= 0 {
|
|
|
|
|
return false
|
|
|
|
|
} else if _length == 1 {
|
|
|
|
|
return val.Index(0).Interface() == search
|
|
|
|
|
}
|
|
|
|
|
mid := _length >> 1
|
|
|
|
|
|
|
|
|
|
for i := 0; i < mid; i++ {
|
2021-09-28 11:47:19 +08:00
|
|
|
|
if val.Index(i).Interface() == search {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2022-03-17 12:11:38 +08:00
|
|
|
|
if val.Index((_length-1)-i).Interface() == search {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if _length%2 > 0 {
|
2022-03-18 13:41:14 +08:00
|
|
|
|
return val.Index(mid).Interface() == search
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ArrayFlip(src interface{}) interface{} {
|
|
|
|
|
val := reflect.ValueOf(src)
|
|
|
|
|
|
|
|
|
|
kind := val.Kind()
|
|
|
|
|
|
|
|
|
|
out := make(map[interface{}]interface{}, 0)
|
|
|
|
|
|
|
|
|
|
if kind == reflect.Slice || kind == reflect.Array || kind == reflect.Map {
|
|
|
|
|
for i := 0; i < val.Len(); i++ {
|
|
|
|
|
fmt.Printf("val.Field():%v\n", val.Index(i).MapKeys())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ArrayStrings(src interface{}) []string {
|
|
|
|
|
out := make([]string, 0)
|
|
|
|
|
|
|
|
|
|
val := reflect.ValueOf(src)
|
|
|
|
|
|
|
|
|
|
kind := val.Kind()
|
|
|
|
|
|
|
|
|
|
if kind == reflect.Slice || kind == reflect.Array {
|
|
|
|
|
for i := 0; i < val.Len(); i++ {
|
|
|
|
|
out = append(out, fmt.Sprintf("%v", val.Index(i).Interface()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
2022-01-14 17:09:06 +08:00
|
|
|
|
|
|
|
|
|
func ArrayUnique(src interface{}) []interface{} {
|
|
|
|
|
val := reflect.ValueOf(src)
|
|
|
|
|
|
|
|
|
|
kind := val.Kind()
|
|
|
|
|
|
|
|
|
|
out := make([]interface{}, 0)
|
|
|
|
|
|
|
|
|
|
if kind == reflect.Slice || kind == reflect.Array {
|
|
|
|
|
for i := 0; i < val.Len(); i++ {
|
|
|
|
|
for _, v := range out {
|
|
|
|
|
if v == val.Index(i).Interface() {
|
|
|
|
|
goto BREAK
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
out = append(out, val.Index(i).Interface())
|
|
|
|
|
BREAK:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|