package common import ( "fmt" "reflect" "strconv" ) func IsIntArrayEqual(x, y []int) bool { if len(x) != len(y) { return false } for i := 0; i < len(x); i++ { if x[i] != y[i] { return false } } return true } func IsIntSliceContains(x int, xs []int) bool { for _, i := range xs { if i == x { return true } } return false } func In(obj interface{}, target interface{}) (bool, error) { targetValue := reflect.ValueOf(target) switch reflect.TypeOf(target).Kind() { case reflect.Slice, reflect.Array: for i := 0; i < targetValue.Len(); i++ { if targetValue.Index(i).Interface() == obj { return true, nil } } case reflect.Map: if targetValue.MapIndex(reflect.ValueOf(obj)).IsValid() { return true, nil } } return false, fmt.Errorf("not in array") } func PSQLIntArray(arr []int) string { str := "{" for i := 0; i < len(arr)-1; i++ { str = str + strconv.Itoa(arr[i]) + "," } str = str + strconv.Itoa(arr[len(arr)-1]) + "}" return str } // IntArrayMinus 返回的zs是在xs中但不在ys中数的集合 func IntArrayMinus(xs, ys []int) (zs []int) { zs = []int{} for _, x := range xs { if !InIntList(x, ys) { zs = append(zs, x) } } return zs } // InStringSliceAnyFunc func InStringSliceAnyFunc(s string, slice []string, matchFunc func(string, string) bool) bool { for _, l := range slice { if matchFunc(s, l) { return true } } return false }