字符串转int相关的slice

This commit is contained in:
白茶清欢 2023-05-04 19:06:10 +08:00
parent 8dbec65861
commit 0c884c1f65
1 changed files with 156 additions and 0 deletions

View File

@ -8,6 +8,8 @@
package util
import (
"strings"
"github.com/pkg/errors"
)
@ -86,6 +88,10 @@ func (sc *stringConvert) ToInt64(str string) (int64, error) {
return 0, nil
}
func (sc *stringConvert) ToInt(str string) (int, error) {
return 0, nil
}
// ToUint8 ...
//
// Author : go_developer@163.com<白茶清欢>
@ -167,3 +173,153 @@ func (sc *stringConvert) ToStruct(str string, receiver interface{}) error {
}
return JSON.UnmarshalWithNumber([]byte(str), receiver)
}
// ToInt8Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:55 2023/5/4
func (sc *stringConvert) ToInt8Slice(str string, splitChar ...string) ([]int8, error) {
var (
err error
res []int8
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]int8, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToInt8(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToInt16Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:01 2023/5/4
func (sc *stringConvert) ToInt16Slice(str string, splitChar ...string) ([]int16, error) {
var (
err error
res []int16
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]int16, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToInt16(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToInt32Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:03 2023/5/4
func (sc *stringConvert) ToInt32Slice(str string, splitChar ...string) ([]int32, error) {
var (
err error
res []int32
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]int32, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToInt32(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToInt64Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:04 2023/5/4
func (sc *stringConvert) ToInt64Slice(str string, splitChar ...string) ([]int64, error) {
var (
err error
res []int64
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]int64, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToInt64(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToIntSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:04 2023/5/4
func (sc *stringConvert) ToIntSlice(str string, splitChar ...string) ([]int, error) {
var (
err error
res []int
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]int, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToInt(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}