github.com/LanceLRQ/deer-common@v0.0.9-0.20210319081233-e8222ac018a8/utils/common.go (about) 1 package utils 2 3 import ( 4 "reflect" 5 ) 6 7 // 判断数组、切片或Map是否存在某个值 8 func Contains(array interface{}, obj interface{}) bool { 9 arrayValue := reflect.ValueOf(array) 10 switch reflect.TypeOf(array).Kind() { 11 case reflect.Slice, reflect.Array: 12 for i := 0; i < arrayValue.Len(); i++ { 13 if arrayValue.Index(i).Interface() == obj { 14 return true 15 } 16 } 17 case reflect.Map: 18 if arrayValue.MapIndex(reflect.ValueOf(obj)).IsValid() { 19 return true 20 } 21 } 22 23 return false 24 }