对string类型的包装

This commit is contained in:
白茶清欢 2023-05-08 18:29:48 +08:00
parent a1748cc324
commit 763009ad36
2 changed files with 393 additions and 268 deletions

140
define.go
View File

@ -203,3 +203,143 @@ type StringResult struct {
Value string
Err error
}
// Int8SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:49 2023/5/8
type Int8SliceResult struct {
Value []int8
Err error
}
// Int16SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:49 2023/5/8
type Int16SliceResult struct {
Value []int16
Err error
}
// Int32SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:50 2023/5/8
type Int32SliceResult struct {
Value []int32
Err error
}
// Int64SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:50 2023/5/8
type Int64SliceResult struct {
Value []int64
Err error
}
// IntSliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:50 2023/5/8
type IntSliceResult struct {
Value []int
Err error
}
// Uint8SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:55 2023/5/8
type Uint8SliceResult struct {
Value []uint8
Err error
}
// Uint16SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:55 2023/5/8
type Uint16SliceResult struct {
Value []uint16
Err error
}
// Uint32SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:55 2023/5/8
type Uint32SliceResult struct {
Value []uint32
Err error
}
// Uint64SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:55 2023/5/8
type Uint64SliceResult struct {
Value []uint64
Err error
}
// UintSliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:56 2023/5/8
type UintSliceResult struct {
Value []uint
Err error
}
// BoolSliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:22 2023/5/8
type BoolSliceResult struct {
Value []bool
Err error
}
// Float32SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:24 2023/5/8
type Float32SliceResult struct {
Value []float32
Err error
}
// Float64SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:24 2023/5/8
type Float64SliceResult struct {
Value []float64
Err error
}
// AnySliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:28 2023/5/8
type AnySliceResult struct {
Value []interface{}
Err error
}

521
string.go
View File

@ -25,13 +25,12 @@ type String string
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:26 2023/5/4
func (str String) ToFloat32() (float32, error) {
func (str String) ToFloat32() Float32Result {
var (
err error
res float32
res Float32Result
)
err = util.ConvertAssign(&res, str)
return res, err
res.Err = util.ConvertAssign(&res.Value, str)
return res
}
// ToFloat64 ...
@ -39,13 +38,12 @@ func (str String) ToFloat32() (float32, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:26 2023/5/4
func (str String) ToFloat64() (float64, error) {
func (str String) ToFloat64() Float64Result {
var (
err error
res float64
res Float64Result
)
err = util.ConvertAssign(&res, str)
return res, err
res.Err = util.ConvertAssign(&res.Value, str)
return res
}
// ToDouble 转double
@ -53,7 +51,7 @@ func (str String) ToFloat64() (float64, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:33 2023/5/4
func (str String) ToDouble() (float64, error) {
func (str String) ToDouble() Float64Result {
return str.ToFloat64()
}
@ -62,7 +60,7 @@ func (str String) ToDouble() (float64, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:33 2023/5/4
func (str String) ToNumber() (float64, error) {
func (str String) ToNumber() Float64Result {
return str.ToFloat64()
}
@ -71,13 +69,12 @@ func (str String) ToNumber() (float64, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToInt8() (int8, error) {
func (str String) ToInt8() Int8Result {
var (
err error
res int8
res Int8Result
)
err = util.ConvertAssign(&res, str)
return res, err
res.Err = util.ConvertAssign(&res.Value, str)
return res
}
// ToInt16 ...
@ -85,13 +82,12 @@ func (str String) ToInt8() (int8, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToInt16() (int16, error) {
func (str String) ToInt16() Int16Result {
var (
err error
res int16
res Int16Result
)
err = util.ConvertAssign(&res, str)
return res, err
res.Err = util.ConvertAssign(&res.Value, str)
return res
}
// ToInt32 ...
@ -99,13 +95,12 @@ func (str String) ToInt16() (int16, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToInt32() (int32, error) {
func (str String) ToInt32() Int32Result {
var (
err error
res int32
res Int32Result
)
err = util.ConvertAssign(&res, str)
return res, err
res.Err = util.ConvertAssign(&res.Value, str)
return res
}
// ToInt64 ...
@ -113,13 +108,12 @@ func (str String) ToInt32() (int32, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToInt64() (int64, error) {
func (str String) ToInt64() Int64Result {
var (
err error
res int64
res Int64Result
)
err = util.ConvertAssign(&res, str)
return res, err
res.Err = util.ConvertAssign(&res.Value, str)
return res
}
// ToInt ...
@ -127,13 +121,12 @@ func (str String) ToInt64() (int64, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:47 2023/5/5
func (str String) ToInt() (int, error) {
func (str String) ToInt() IntResult {
var (
err error
res int
res IntResult
)
err = util.ConvertAssign(&res, str)
return res, err
res.Err = util.ConvertAssign(&res.Value, str)
return res
}
// ToUint8 ...
@ -141,13 +134,12 @@ func (str String) ToInt() (int, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:30 2023/5/4
func (str String) ToUint8() (uint8, error) {
func (str String) ToUint8() Uint8Result {
var (
err error
res uint8
res Uint8Result
)
err = util.ConvertAssign(&res, str)
return res, err
res.Err = util.ConvertAssign(&res.Value, str)
return res
}
// ToUint16 ...
@ -155,13 +147,12 @@ func (str String) ToUint8() (uint8, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToUint16() (uint16, error) {
func (str String) ToUint16() Uint16Result {
var (
err error
res uint16
res Uint16Result
)
err = util.ConvertAssign(&res, str)
return res, err
res.Err = util.ConvertAssign(&res.Value, str)
return res
}
// ToUint32 ...
@ -169,13 +160,12 @@ func (str String) ToUint16() (uint16, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToUint32() (uint32, error) {
func (str String) ToUint32() Uint32Result {
var (
err error
res uint32
res Uint32Result
)
err = util.ConvertAssign(&res, str)
return res, err
res.Err = util.ConvertAssign(&res.Value, str)
return res
}
// ToUint64 ...
@ -183,13 +173,12 @@ func (str String) ToUint32() (uint32, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToUint64() (uint64, error) {
func (str String) ToUint64() Uint64Result {
var (
err error
res uint64
res Uint64Result
)
err = util.ConvertAssign(&res, str)
return res, err
res.Err = util.ConvertAssign(&res.Value, str)
return res
}
// ToUint ...
@ -197,13 +186,12 @@ func (str String) ToUint64() (uint64, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToUint() (uint, error) {
func (str String) ToUint() UintResult {
var (
err error
res uint
res UintResult
)
err = util.ConvertAssign(&res, str)
return res, err
res.Err = util.ConvertAssign(&res.Value, str)
return res
}
// ToBool ...
@ -211,13 +199,12 @@ func (str String) ToUint() (uint, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:34 2023/5/4
func (str String) ToBool() (bool, error) {
func (str String) ToBool() BoolResult {
var (
err error
res bool
res BoolResult
)
err = util.ConvertAssign(&res, str)
return res, err
res.Err = util.ConvertAssign(&res.Value, str)
return res
}
// ToObject ...
@ -225,15 +212,15 @@ func (str String) ToBool() (bool, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:35 2023/5/4
func (str String) ToObject() (map[string]interface{}, error) {
func (str String) ToObject() ObjectResult {
var (
err error
result map[string]interface{}
res = ObjectResult{
Value: map[string]interface{}{},
Err: nil,
}
)
if err = util.JSON.UnmarshalWithNumber([]byte(str), &result); nil != err {
return nil, err
}
return result, nil
res.Err = util.JSON.UnmarshalWithNumber([]byte(str), &res.Value)
return res
}
// ToStruct ...
@ -253,29 +240,31 @@ func (str String) ToStruct(receiver interface{}) error {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:55 2023/5/4
func (str String) ToInt8Slice(splitChar ...string) ([]int8, error) {
var (
err error
res []int8
)
func (str String) ToInt8Slice(splitChar ...string) Int8SliceResult {
result := Int8SliceResult{
Value: []int8{},
Err: nil,
}
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}
res = make([]int8, 0)
var (
res = make([]int8, 0)
)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToInt8(); nil != err {
return nil, err
if itemVal := String(item).ToInt8(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
} else {
res = append(res, itemVal)
res = append(res, itemVal.Value)
}
}
return res, nil
result.Value = res
return result
}
// ToInt16Slice ...
@ -283,29 +272,30 @@ func (str String) ToInt8Slice(splitChar ...string) ([]int8, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:01 2023/5/4
func (str String) ToInt16Slice(splitChar ...string) ([]int16, error) {
var (
err error
res []int16
)
func (str String) ToInt16Slice(splitChar ...string) Int16SliceResult {
result := Int16SliceResult{
Value: []int16{},
Err: nil,
}
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}
res = make([]int16, 0)
res := make([]int16, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToInt16(); nil != err {
return nil, err
if itemVal := String(item).ToInt16(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
} else {
res = append(res, itemVal)
res = append(res, itemVal.Value)
}
}
return res, nil
result.Value = res
return result
}
// ToInt32Slice ...
@ -313,29 +303,29 @@ func (str String) ToInt16Slice(splitChar ...string) ([]int16, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:03 2023/5/4
func (str String) ToInt32Slice(splitChar ...string) ([]int32, error) {
var (
err error
res []int32
)
func (str String) ToInt32Slice(splitChar ...string) Int32SliceResult {
result := Int32SliceResult{
Value: []int32{},
Err: nil,
}
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}
res = make([]int32, 0)
res := make([]int32, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToInt32(); nil != err {
return nil, err
if itemVal := String(item).ToInt32(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
} else {
res = append(res, itemVal)
res = append(res, itemVal.Value)
}
}
return res, nil
result.Value = res
return result
}
// ToInt64Slice ...
@ -343,29 +333,28 @@ func (str String) ToInt32Slice(splitChar ...string) ([]int32, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:04 2023/5/4
func (str String) ToInt64Slice(splitChar ...string) ([]int64, error) {
var (
err error
res []int64
)
func (str String) ToInt64Slice(splitChar ...string) Int64SliceResult {
result := Int64SliceResult{
Value: []int64{},
Err: nil,
}
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}
res = make([]int64, 0)
res := make([]int64, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToInt64(); nil != err {
return nil, err
if itemVal := String(item).ToInt64(); nil != itemVal.Err {
return result
} else {
res = append(res, itemVal)
res = append(res, itemVal.Value)
}
}
return res, nil
result.Value = res
return result
}
// ToIntSlice ...
@ -373,29 +362,29 @@ func (str String) ToInt64Slice(splitChar ...string) ([]int64, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:04 2023/5/4
func (str String) ToIntSlice(splitChar ...string) ([]int, error) {
var (
err error
res []int
)
func (str String) ToIntSlice(splitChar ...string) IntSliceResult {
result := IntSliceResult{
Value: []int{},
Err: nil,
}
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}
res = make([]int, 0)
res := make([]int, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToInt(); nil != err {
return nil, err
if itemVal := String(item).ToInt(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
} else {
res = append(res, itemVal)
res = append(res, itemVal.Value)
}
}
return res, nil
result.Value = res
return result
}
// ToUint8Slice ...
@ -403,29 +392,29 @@ func (str String) ToIntSlice(splitChar ...string) ([]int, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:50 2023/5/5
func (str String) ToUint8Slice(splitChar ...string) ([]uint8, error) {
var (
err error
res []uint8
)
func (str String) ToUint8Slice(splitChar ...string) Uint8SliceResult {
result := Uint8SliceResult{
Value: []uint8{},
Err: nil,
}
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}
res = make([]uint8, 0)
res := make([]uint8, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToUint8(); nil != err {
return nil, err
if itemVal := String(item).ToUint8(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
} else {
res = append(res, itemVal)
res = append(res, itemVal.Value)
}
}
return res, nil
result.Value = res
return result
}
// ToUint16Slice ...
@ -433,29 +422,29 @@ func (str String) ToUint8Slice(splitChar ...string) ([]uint8, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:52 2023/5/5
func (str String) ToUint16Slice(splitChar ...string) ([]uint16, error) {
var (
err error
res []uint16
)
func (str String) ToUint16Slice(splitChar ...string) Uint16SliceResult {
result := Uint16SliceResult{
Value: []uint16{},
Err: nil,
}
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}
res = make([]uint16, 0)
res := make([]uint16, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToUint16(); nil != err {
return nil, err
if itemVal := String(item).ToUint16(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
} else {
res = append(res, itemVal)
res = append(res, itemVal.Value)
}
}
return res, nil
result.Value = res
return result
}
// ToUint32Slice ...
@ -463,29 +452,29 @@ func (str String) ToUint16Slice(splitChar ...string) ([]uint16, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:53 2023/5/5
func (str String) ToUint32Slice(splitChar ...string) ([]uint32, error) {
var (
err error
res []uint32
)
func (str String) ToUint32Slice(splitChar ...string) Uint32SliceResult {
result := Uint32SliceResult{
Value: []uint32{},
Err: nil,
}
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}
res = make([]uint32, 0)
res := make([]uint32, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToUint32(); nil != err {
return nil, err
if itemVal := String(item).ToUint32(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
} else {
res = append(res, itemVal)
res = append(res, itemVal.Value)
}
}
return res, nil
result.Value = res
return result
}
// ToUint64Slice ...
@ -493,29 +482,28 @@ func (str String) ToUint32Slice(splitChar ...string) ([]uint32, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:54 2023/5/5
func (str String) ToUint64Slice(splitChar ...string) ([]uint64, error) {
var (
err error
res []uint64
)
func (str String) ToUint64Slice(splitChar ...string) Uint64SliceResult {
result := Uint64SliceResult{
Value: []uint64{},
Err: nil,
}
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}
res = make([]uint64, 0)
res := make([]uint64, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToUint64(); nil != err {
return nil, err
if itemVal := String(item).ToUint64(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
} else {
res = append(res, itemVal)
res = append(res, itemVal.Value)
}
}
return res, nil
return result
}
// ToUintSlice ...
@ -523,29 +511,29 @@ func (str String) ToUint64Slice(splitChar ...string) ([]uint64, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:54 2023/5/5
func (str String) ToUintSlice(splitChar ...string) ([]uint, error) {
var (
err error
res []uint
)
func (str String) ToUintSlice(splitChar ...string) UintSliceResult {
result := UintSliceResult{
Value: []uint{},
Err: nil,
}
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}
res = make([]uint, 0)
res := make([]uint, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToUint(); nil != err {
return nil, err
if itemVal := String(item).ToUint(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
} else {
res = append(res, itemVal)
res = append(res, itemVal.Value)
}
}
return res, nil
result.Value = res
return result
}
// ToBoolSlice ...
@ -553,29 +541,29 @@ func (str String) ToUintSlice(splitChar ...string) ([]uint, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:55 2023/5/5
func (str String) ToBoolSlice(splitChar ...string) ([]bool, error) {
var (
err error
res []bool
)
func (str String) ToBoolSlice(splitChar ...string) BoolSliceResult {
result := BoolSliceResult{
Value: []bool{},
Err: nil,
}
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}
res = make([]bool, 0)
res := make([]bool, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToBool(); nil != err {
return nil, err
if itemVal := String(item).ToBool(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
} else {
res = append(res, itemVal)
res = append(res, itemVal.Value)
}
}
return res, nil
result.Value = res
return result
}
// ToFloat32Slice ...
@ -583,29 +571,28 @@ func (str String) ToBoolSlice(splitChar ...string) ([]bool, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:56 2023/5/5
func (str String) ToFloat32Slice(splitChar ...string) ([]float32, error) {
var (
err error
res []float32
)
func (str String) ToFloat32Slice(splitChar ...string) Float32SliceResult {
result := Float32SliceResult{
Value: []float32{},
Err: nil,
}
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}
res = make([]float32, 0)
res := make([]float32, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToFloat32(); nil != err {
return nil, err
if itemVal := String(item).ToFloat32(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
} else {
res = append(res, itemVal)
res = append(res, itemVal.Value)
}
}
return res, nil
return result
}
// ToFloat64Slice ...
@ -613,29 +600,29 @@ func (str String) ToFloat32Slice(splitChar ...string) ([]float32, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:56 2023/5/5
func (str String) ToFloat64Slice(splitChar ...string) ([]float64, error) {
var (
err error
res []float64
)
func (str String) ToFloat64Slice(splitChar ...string) Float64SliceResult {
result := Float64SliceResult{
Value: []float64{},
Err: nil,
}
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}
res = make([]float64, 0)
res := make([]float64, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToFloat64(); nil != err {
return nil, err
if itemVal := String(item).ToFloat64(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
} else {
res = append(res, itemVal)
res = append(res, itemVal.Value)
}
}
return res, nil
result.Value = res
return result
}
// ToDoubleSlice ...
@ -643,7 +630,7 @@ func (str String) ToFloat64Slice(splitChar ...string) ([]float64, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:57 2023/5/5
func (str String) ToDoubleSlice(splitChar ...string) ([]float64, error) {
func (str String) ToDoubleSlice(splitChar ...string) Float64SliceResult {
return str.ToFloat64Slice(splitChar...)
}
@ -652,7 +639,7 @@ func (str String) ToDoubleSlice(splitChar ...string) ([]float64, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:57 2023/5/5
func (str String) ToNumberSlice(splitChar ...string) ([]float64, error) {
func (str String) ToNumberSlice(splitChar ...string) Float64SliceResult {
return str.ToFloat64Slice(splitChar...)
}
@ -661,14 +648,12 @@ func (str String) ToNumberSlice(splitChar ...string) ([]float64, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:01 2023/5/5
func (str String) ToAnySlice() ([]interface{}, error) {
var (
err error
res []interface{}
)
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
func (str String) ToAnySlice() AnySliceResult {
result := AnySliceResult{
Value: []interface{}{},
Err: nil,
}
return res, nil
result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}