2025-02-06 11:14:33 +08:00
|
|
|
|
package utility
|
|
|
|
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
|
|
|
|
|
|
// ContainsStr []string 包含元素?
|
|
|
|
|
|
func ContainsStr(arr []string, v string) bool {
|
|
|
|
|
|
for _, a := range arr {
|
|
|
|
|
|
if a == v {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func RemoveByValue(slice []string, value string) []string {
|
|
|
|
|
|
for i, v := range slice {
|
|
|
|
|
|
if v == value {
|
|
|
|
|
|
// 找到值,删除对应索引
|
|
|
|
|
|
return append(slice[:i], slice[i+1:]...)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return slice // 未找到返回原切片
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ContainsInt []int 包含元素?
|
|
|
|
|
|
func ContainsInt(arr []int, v int) bool {
|
|
|
|
|
|
for _, a := range arr {
|
|
|
|
|
|
if a == v {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func HasSuffix(data string, suffixs []string) bool {
|
|
|
|
|
|
for _, suffix := range suffixs {
|
|
|
|
|
|
if strings.HasSuffix(data, suffix) {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SplitSlice 将 []string 切片根据最大数量分割成二维数组
|
2025-03-19 09:38:11 +08:00
|
|
|
|
func SplitSlice[T any](slice []T, maxSize int) [][]T {
|
|
|
|
|
|
var result [][]T
|
2025-02-06 11:14:33 +08:00
|
|
|
|
|
|
|
|
|
|
// 遍历切片,每次取 maxSize 个元素
|
|
|
|
|
|
for i := 0; i < len(slice); i += maxSize {
|
|
|
|
|
|
end := i + maxSize
|
|
|
|
|
|
// 如果 end 超出切片长度,则取到切片末尾
|
|
|
|
|
|
if end > len(slice) {
|
|
|
|
|
|
end = len(slice)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 将当前段添加到结果中
|
|
|
|
|
|
result = append(result, slice[i:end])
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//// 切片;
|
|
|
|
|
|
//type slices struct {
|
|
|
|
|
|
//}
|
|
|
|
|
|
//
|
|
|
|
|
|
//// 构建;
|
|
|
|
|
|
//func Slices() *slices {
|
|
|
|
|
|
// return &slices{}
|
|
|
|
|
|
//}
|
|
|
|
|
|
//
|
|
|
|
|
|
//// 包含元素?
|
|
|
|
|
|
//func (this *slices) Contains(s interface{}, v interface{}) bool {
|
|
|
|
|
|
// // 相关定义;
|
|
|
|
|
|
// ss := reflect.Indirect(reflect.ValueOf(s))
|
|
|
|
|
|
// slen := ss.Len()
|
|
|
|
|
|
// // 遍历;
|
|
|
|
|
|
// for i := 0; i < slen; i++ {
|
|
|
|
|
|
// // 定位元素;
|
|
|
|
|
|
// sv := reflect.Indirect(ss.Index(i))
|
|
|
|
|
|
// if fmt.Sprint(sv.Interface()) == fmt.Sprint(v) {
|
|
|
|
|
|
// return true
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// return false
|
|
|
|
|
|
//}
|
|
|
|
|
|
//
|
|
|
|
|
|
//// 转为切片;
|
|
|
|
|
|
//func (this *slices) Slice(s interface{}) []interface{} {
|
|
|
|
|
|
// // 相关定义;
|
|
|
|
|
|
// ss := reflect.Indirect(reflect.ValueOf(s))
|
|
|
|
|
|
// slen := ss.Len()
|
|
|
|
|
|
// // 遍历;
|
|
|
|
|
|
// out := make([]interface{}, slen)
|
|
|
|
|
|
// for i := 0; i < slen; i++ {
|
|
|
|
|
|
// // 追加;
|
|
|
|
|
|
// out[i] = ss.Index(i).Interface()
|
|
|
|
|
|
// }
|
|
|
|
|
|
// return out
|
|
|
|
|
|
//}
|
|
|
|
|
|
//
|
|
|
|
|
|
//// 校验为nil?
|
|
|
|
|
|
//func (this *slices) IsNil(v interface{}) bool {
|
|
|
|
|
|
// if v == nil {
|
|
|
|
|
|
// return true
|
|
|
|
|
|
// }
|
|
|
|
|
|
// vi := reflect.ValueOf(v)
|
|
|
|
|
|
// if vi.Kind() == reflect.Ptr {
|
|
|
|
|
|
// return vi.Elem().IsNil()
|
|
|
|
|
|
// }
|
|
|
|
|
|
// return vi.IsNil()
|
|
|
|
|
|
//}
|
|
|
|
|
|
//
|
|
|
|
|
|
//// StringSliceReflectEqual 判断 string和slice 是否相等
|
|
|
|
|
|
//// 因为使用了反射,所以效率较低,可以看benchmark结果
|
|
|
|
|
|
//func (this *slices) StringSliceReflectEqual(a, b []string) bool {
|
|
|
|
|
|
// return reflect.DeepEqual(a, b)
|
|
|
|
|
|
//}
|
|
|
|
|
|
//
|
|
|
|
|
|
//// StringSliceEqual 判断 string和slice 是否相等
|
|
|
|
|
|
//// 使用了传统的遍历方式
|
|
|
|
|
|
//func (this *slices) StringSliceEqual(a, b []string) bool {
|
|
|
|
|
|
// if len(a) != len(b) {
|
|
|
|
|
|
// return false
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
// // reflect.DeepEqual的结果保持一致
|
|
|
|
|
|
// if (a == nil) != (b == nil) {
|
|
|
|
|
|
// return false
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
// // bounds check 边界检查
|
|
|
|
|
|
// // 避免越界
|
|
|
|
|
|
// b = b[:len(a)]
|
|
|
|
|
|
// for i, v := range a {
|
|
|
|
|
|
// if v != b[i] {
|
|
|
|
|
|
// return false
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
// return true
|
|
|
|
|
|
//}
|