fusenapi/utils/format/str_to_int_slice.go
laodaming c1de53c098 fix
2023-07-05 18:08:18 +08:00

38 lines
724 B
Go

package format
import (
"strconv"
)
// 字符串切片转int切片
func StrSlicToIntSlice(input []string) ([]int, error) {
newSlic := make([]int, 0, len(input))
for _, element := range input {
if element == "" {
continue
}
val, err := strconv.Atoi(element)
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 _, element := range input {
if element == "" {
continue
}
val, err := strconv.ParseInt(element, 10, 64)
if err != nil {
return nil, err
}
newSlic = append(newSlic, val)
}
return newSlic, nil
}