fusenapi/utils/format/str_to_int_slice.go

38 lines
724 B
Go
Raw Normal View History

2023-06-05 02:18:31 +00:00
package format
import (
"strconv"
)
// 字符串切片转int切片
func StrSlicToIntSlice(input []string) ([]int, error) {
2023-06-21 04:11:43 +00:00
newSlic := make([]int, 0, len(input))
2023-07-05 10:08:18 +00:00
for _, element := range input {
if element == "" {
2023-06-05 02:18:31 +00:00
continue
}
2023-07-05 10:08:18 +00:00
val, err := strconv.Atoi(element)
2023-06-05 02:18:31 +00:00
if err != nil {
return nil, err
}
2023-06-21 04:11:43 +00:00
newSlic = append(newSlic, val)
2023-06-05 02:18:31 +00:00
}
2023-06-21 04:11:43 +00:00
return newSlic, nil
}
// 字符串切片转int64切片
func StrSlicToInt64Slice(input []string) ([]int64, error) {
newSlic := make([]int64, 0, len(input))
2023-07-05 10:08:18 +00:00
for _, element := range input {
if element == "" {
2023-06-21 04:11:43 +00:00
continue
}
2023-07-05 10:08:18 +00:00
val, err := strconv.ParseInt(element, 10, 64)
2023-06-21 04:11:43 +00:00
if err != nil {
return nil, err
}
newSlic = append(newSlic, val)
}
return newSlic, nil
2023-06-05 02:18:31 +00:00
}