fusenapi/utils/format/str_to_int_slice.go
laodaming db9c2f3cc7 fix
2023-06-21 12:11:43 +08:00

38 lines
688 B
Go

package format
import (
"strconv"
)
// 字符串切片转int切片
func StrSlicToIntSlice(input []string) ([]int, error) {
newSlic := make([]int, 0, len(input))
for _, p := range input {
if p == "" {
continue
}
val, err := strconv.Atoi(p)
if err != nil {
return nil, err
}
newSlic = append(newSlic, val)
}
return newSlic, nil
}
// 字符串切片转int64切片
func StrSlicToInt64Slice(input []string) ([]int64, error) {
newSlic := make([]int64, 0, len(input))
for _, p := range input {
if p == "" {
continue
}
val, err := strconv.ParseInt(p, 10, 64)
if err != nil {
return nil, err
}
newSlic = append(newSlic, val)
}
return newSlic, nil
}