Files
2022-03-18 13:41:14 +08:00

91 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import (
"fmt"
"reflect"
)
// [1,2,3, 4, 5,6,7]
// 7
// mid: 3
func InArray(search, needle interface{}) bool {
val := reflect.ValueOf(needle)
kind := val.Kind()
if kind == reflect.Slice || kind == reflect.Array {
_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++ {
if val.Index(i).Interface() == search {
return true
}
if val.Index((_length-1)-i).Interface() == search {
return true
}
}
if _length%2 > 0 {
return val.Index(mid).Interface() == search
}
}
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
}
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
}