golang 实现类型php中is_arrray()函数

# golang / 102访问 / 0评论 / 2021-09-13
//obj 字符 target  数组
func InArray(obj interface{}, target interface{}) (bool) {
	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
			}
		}
	case reflect.Map:
		if targetValue.MapIndex(reflect.ValueOf(obj)).IsValid() {
			return true
		}
	}
	return false
}
1

本文地址: https://www.kumaomao.cn/article/OGHIMMGNIE.html