From 94707fcb3e21b39c2991afbbaa561525a62f453f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 8 May 2023 16:59:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96int=E5=8C=85=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ int.go | 85 ++++++++++++++++++++--------- 2 files changed, 220 insertions(+), 25 deletions(-) diff --git a/define.go b/define.go index 3e14fcd..50beb06 100644 --- a/define.go +++ b/define.go @@ -43,3 +43,163 @@ const ( DataTypeObject = "map[string]interface" DataTypeAnyObject = "map[interface]interface" ) + +// Int8Result ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:25 2023/5/8 +type Int8Result struct { + Value int8 + Err error +} + +// Int16Result ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:26 2023/5/8 +type Int16Result struct { + Value int16 + Err error +} + +// Int32Result ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:26 2023/5/8 +type Int32Result struct { + Value int32 + Err error +} + +// Int64Result ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:26 2023/5/8 +type Int64Result struct { + Value int64 + Err error +} + +// IntResult ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:26 2023/5/8 +type IntResult struct { + Value int + Err error +} + +// Uint8Result ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:28 2023/5/8 +type Uint8Result struct { + Value uint8 + Err error +} + +// Uint16Result ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:28 2023/5/8 +type Uint16Result struct { + Value uint16 + Err error +} + +// Uint32Result ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:28 2023/5/8 +type Uint32Result struct { + Value uint32 + Err error +} + +// Uint64Result ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:28 2023/5/8 +type Uint64Result struct { + Value uint64 + Err error +} + +// UintResult ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:29 2023/5/8 +type UintResult struct { + Value uint + Err error +} + +// Float32Result ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:31 2023/5/8 +type Float32Result struct { + Value float32 + Err error +} + +// Float64Result ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:32 2023/5/8 +type Float64Result struct { + Value float64 + Err error +} + +// Any ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:40 2023/5/8 +type Any struct { + Value interface{} + Err error +} + +// BoolResult ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:36 2023/5/8 +type BoolResult struct { + Value bool + Err error +} + +// ObjectResult ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:38 2023/5/8 +type ObjectResult struct { + Value map[string]interface{} + Err error +} + +// StringResult ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:57 2023/5/8 +type StringResult struct { + Value string + Err error +} diff --git a/int.go b/int.go index d581cc4..29cb8cb 100644 --- a/int.go +++ b/int.go @@ -24,11 +24,17 @@ type Int int64 // Author : go_developer@163.com<白茶清欢> // // Date : 13:57 2023/5/5 -func (i Int) ToInt8() (int8, error) { - if math.MaxInt8 < int64(i) || math.MinInt8 > int64(i) { - return 0, fmt.Errorf("int8 value should between %v and %v ", int8(math.MinInt8), int8(math.MaxInt8)) +func (i Int) ToInt8() Int8Result { + res := Int8Result{ + Value: 0, + Err: nil, } - return int8(i), nil + if math.MaxInt8 < int64(i) || math.MinInt8 > int64(i) { + res.Err = fmt.Errorf("int8 value should between %v and %v ", int8(math.MinInt8), int8(math.MaxInt8)) + return res + } + res.Value = int8(i) + return res } // ToInt16 ... @@ -36,11 +42,17 @@ func (i Int) ToInt8() (int8, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 14:05 2023/5/5 -func (i Int) ToInt16() (int16, error) { - if math.MaxInt16 < int64(i) || math.MinInt16 > int64(i) { - return 0, fmt.Errorf("int16 value should between %v and %v ", int16(math.MinInt16), int16(math.MaxInt16)) +func (i Int) ToInt16() Int16Result { + res := Int16Result{ + Value: 0, + Err: nil, } - return int16(i), nil + if math.MaxInt16 < int64(i) || math.MinInt16 > int64(i) { + res.Err = fmt.Errorf("int16 value should between %v and %v ", int16(math.MinInt16), int16(math.MaxInt16)) + return res + } + res.Value = int16(i) + return res } // ToInt32 ... @@ -48,11 +60,17 @@ func (i Int) ToInt16() (int16, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 14:05 2023/5/5 -func (i Int) ToInt32() (int32, error) { - if math.MaxInt32 < int64(i) || math.MinInt32 > int64(i) { - return 0, fmt.Errorf("int32 value should between %v and %v ", int32(math.MinInt32), int32(math.MaxInt32)) +func (i Int) ToInt32() Int32Result { + res := Int32Result{ + Value: 0, + Err: nil, } - return int32(i), nil + if math.MaxInt32 < int64(i) || math.MinInt32 > int64(i) { + res.Err = fmt.Errorf("int32 value should between %v and %v ", int32(math.MinInt32), int32(math.MaxInt32)) + return res + } + res.Value = int32(i) + return res } // ToInt64 ... @@ -60,11 +78,17 @@ func (i Int) ToInt32() (int32, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 14:06 2023/5/5 -func (i Int) ToInt64() (int64, error) { - if math.MaxInt64 < i || math.MinInt64 > i { - return 0, fmt.Errorf("int64 value should between %v and %v ", int64(math.MinInt64), int64(math.MaxInt64)) +func (i Int) ToInt64() Int64Result { + res := Int64Result{ + Value: 0, + Err: nil, } - return int64(i), nil + if math.MaxInt64 < i || math.MinInt64 > i { + res.Err = fmt.Errorf("int64 value should between %v and %v ", int64(math.MinInt64), int64(math.MaxInt64)) + return res + } + res.Value = int64(i) + return res } // ToInt ... @@ -72,11 +96,16 @@ func (i Int) ToInt64() (int64, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 14:07 2023/5/5 -func (i Int) ToInt() (int, error) { - if math.MaxInt < i || math.MinInt > i { - return 0, fmt.Errorf("int value should between %v and %v ", int(math.MinInt), int(math.MaxInt)) +func (i Int) ToInt() IntResult { + res := IntResult{ + Value: 0, + Err: nil, } - return int(i), nil + if math.MaxInt < i || math.MinInt > i { + res.Err = fmt.Errorf("int value should between %v and %v ", int(math.MinInt), int(math.MaxInt)) + } + res.Value = int(i) + return res } // ToString ... @@ -84,10 +113,16 @@ func (i Int) ToInt() (int, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 14:07 2023/5/5 -func (i Int) ToString() (string, error) { - in64Val, err := i.ToInt64() - if nil != err { - return "", err +func (i Int) ToString() StringResult { + result := i.ToInt64() + if nil != result.Err { + return StringResult{ + Value: "", + Err: result.Err, + } + } + return StringResult{ + Value: fmt.Sprintf("%v", result.Value), + Err: nil, } - return fmt.Sprintf("%v", in64Val), nil }