1 Star 0 Fork 0

Eriloan / goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
reflect.go 560 Bytes
一键复制 编辑 原始数据 按行查看 历史
Eriloan 提交于 2022-03-23 17:34 . 测试tags
package os
import (
"fmt"
"reflect"
)
//Contains 判断 source 是否包含在target中,target支持类型:Array/Slice/Map
func Contains(source interface{}, target interface{}) (bool, error) {
t := reflect.ValueOf(target)
switch reflect.TypeOf(target).Kind() {
case reflect.Slice, reflect.Array:
for i := 0; i < t.Len(); i++ {
if t.Index(i).Interface() == source {
return true, nil
}
}
case reflect.Map:
if t.MapIndex(reflect.ValueOf(source)).IsValid() {
return true, nil
}
}
return false, fmt.Errorf("%s", "Invalid array")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/shibingli/goutils.git
git@gitee.com:shibingli/goutils.git
shibingli
goutils
goutils
v1.2.5

搜索帮助

344bd9b3 5694891 D2dac590 5694891