go 字符串切割转int32数组/切片

输入:字符串str, 分隔符sep
输出:int32类型切片 i32List

import(
	"strings"
	"strconv"
)
func SplitToInt32List(str string, sep string)(i32List []int32){
	if str == ""{
		return
	}
	strList := strings.Split(str, sep)
	if len(strList) == 0{
		return
	}
	for _, item := range strList{
		if item == ""{
			continue
		}
		val, err := strconv.ParseInt(item, 10, 32)
		if err != nil{
			// logs.CtxError(ctx, "ParseInt fail, err=%v, str=%v, sep=%v", err, str, sep) // 此处打印出log报错信息
			continue
		}
		i32List = append(i32List, int32(val))
	} 
}

你可能感兴趣的:(Go,go)