代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。