1 Star 0 Fork 0

filters/utils

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
contains.go 2.51 KB
一键复制 编辑 原始数据 按行查看 历史
v_lqiaolei 提交于 2025-06-21 12:04 +08:00 . 更新
package character
import (
"github.com/spf13/cast"
"reflect"
)
type Contains interface {
ContainInterface(arr []interface{}, str interface{}) bool
ContainString(arr []string, str string) bool
ContainInt32(arr []int32, str int32) bool
ContainInt64(arr []int64, str int64) bool
ContainFloat64(arr []float64, str float64) bool
}
type Container struct {
}
func NewContain() *Container {
return &Container{}
}
// ContainInterface ...
func (c *Container) ContainInterface(arr interface{}, str interface{}) bool {
val := reflect.ValueOf(arr)
if val.Kind() != reflect.Slice {
return false
}
for i := 0; i < val.Len(); i++ {
elem := val.Index(i)
// 根据元素类型进行处理
switch elem.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if elem.Int() == cast.ToInt64(str) {
return true
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if elem.Uint() == cast.ToUint64(str) {
return true
}
case reflect.Float32, reflect.Float64:
if elem.Float() == cast.ToFloat64(str) {
return true
}
case reflect.String:
if elem.String() == cast.ToString(str) {
return true
}
case reflect.Bool:
if elem.Bool() == cast.ToBool(str) {
return true
}
default:
if elem.Interface() == str {
return true
}
}
}
return false
}
// ContainString 检查字符串数组中是否包含指定的字符串
func (c *Container) ContainString(arr []string, str string) bool {
for _, value := range arr {
if value == str {
return true // 找到了,返回true
}
}
return false // 遍历完数组都没有找到,返回false
}
// ContainInt32 检查字符串数组中是否包含指定的字符串
func (c *Container) ContainInt32(arr []int32, str int32) bool {
for _, value := range arr {
if value == str {
return true // 找到了,返回true
}
}
return false // 遍历完数组都没有找到,返回false
}
// ContainInt64 检查字符串数组中是否包含指定的字符串
func (c *Container) ContainInt64(arr []int64, str int64) bool {
for _, value := range arr {
if value == str {
return true // 找到了,返回true
}
}
return false // 遍历完数组都没有找到,返回false
}
// ContainFloat64 检查字符串数组中是否包含指定的字符串
func (c *Container) ContainFloat64(arr []float64, str float64) bool {
for _, value := range arr {
if value == str {
return true // 找到了,返回true
}
}
return false // 遍历完数组都没有找到,返回false
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/filters/utils.git
git@gitee.com:filters/utils.git
filters
utils
utils
master

搜索帮助