95 lines
1.6 KiB
Go
95 lines
1.6 KiB
Go
package utils
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/spf13/cast"
|
|
)
|
|
|
|
// 拆分Uid
|
|
func UIdSplit(uid string) (string, string, bool) {
|
|
s := strings.SplitN(uid, "_", 2)
|
|
if len(s) < 2 {
|
|
return "", "", false
|
|
}
|
|
|
|
return s[0], s[1], true
|
|
}
|
|
|
|
func Findx[T string | int32](slice []T, val T) (int, bool) {
|
|
for i, item := range slice {
|
|
if item == val {
|
|
return i, true
|
|
}
|
|
}
|
|
return -1, false
|
|
}
|
|
|
|
// Deprecated: Use Findx instead
|
|
func Find(slice []string, val string) (int, bool) {
|
|
for i, item := range slice {
|
|
if item == val {
|
|
return i, true
|
|
}
|
|
}
|
|
return -1, false
|
|
}
|
|
|
|
func Deletex[T string | int32](slice []T, val T) []T {
|
|
result := make([]T, 0)
|
|
for _, v := range slice {
|
|
if v != val {
|
|
result = append(result, v)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// Deprecated: Use Deletex instead
|
|
func DeleteString(list []string, ele string) []string {
|
|
result := make([]string, 0)
|
|
for _, v := range list {
|
|
if v != ele {
|
|
result = append(result, v)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
//转换包含数字字符串数组
|
|
//eg. "3,4,5" to []int32{3,4,5}
|
|
func TrInt32(val string) (nums []int32) {
|
|
strArr := strings.Split(val, ",")
|
|
if len(strArr) == 0 {
|
|
return
|
|
}
|
|
|
|
for _, n := range strArr {
|
|
if IsNum(strings.TrimSpace(n)) {
|
|
nums = append(nums, cast.ToInt32(strings.TrimSpace(n)))
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func IsNum(s string) bool {
|
|
_, err := strconv.ParseFloat(s, 64)
|
|
return err == nil
|
|
}
|
|
|
|
func ToInt32(s string) int32 {
|
|
if j, err := strconv.ParseInt(s, 10, 32); err != nil {
|
|
return 0
|
|
} else {
|
|
return int32(j)
|
|
}
|
|
}
|
|
|
|
func ToInt64(s string) int64 {
|
|
if bint, err := strconv.ParseInt(s, 10, 32); err == nil {
|
|
return bint
|
|
}
|
|
return 0
|
|
}
|