包装uint

This commit is contained in:
白茶清欢 2023-05-05 14:32:17 +08:00
parent dfce652f5c
commit 43448d1001
2 changed files with 93 additions and 4 deletions

8
int.go
View File

@ -38,7 +38,7 @@ func (i Int) ToInt8() (int8, error) {
// Date : 14:05 2023/5/5 // Date : 14:05 2023/5/5
func (i Int) ToInt16() (int16, error) { func (i Int) ToInt16() (int16, error) {
if math.MaxInt16 < int64(i) || math.MinInt16 > int64(i) { if math.MaxInt16 < int64(i) || math.MinInt16 > int64(i) {
return 0, fmt.Errorf("int16 value should between %v and %v ", math.MinInt8, math.MaxInt8) return 0, fmt.Errorf("int16 value should between %v and %v ", math.MinInt16, math.MaxInt16)
} }
return int16(i), nil return int16(i), nil
} }
@ -50,7 +50,7 @@ func (i Int) ToInt16() (int16, error) {
// Date : 14:05 2023/5/5 // Date : 14:05 2023/5/5
func (i Int) ToInt32() (int32, error) { func (i Int) ToInt32() (int32, error) {
if math.MaxInt32 < int64(i) || math.MinInt32 > int64(i) { if math.MaxInt32 < int64(i) || math.MinInt32 > int64(i) {
return 0, fmt.Errorf("int32 value should between %v and %v ", math.MinInt8, math.MaxInt8) return 0, fmt.Errorf("int32 value should between %v and %v ", math.MinInt32, math.MaxInt32)
} }
return int32(i), nil return int32(i), nil
} }
@ -62,7 +62,7 @@ func (i Int) ToInt32() (int32, error) {
// Date : 14:06 2023/5/5 // Date : 14:06 2023/5/5
func (i Int) ToInt64() (int64, error) { func (i Int) ToInt64() (int64, error) {
if math.MaxInt64 < i || math.MinInt64 > i { if math.MaxInt64 < i || math.MinInt64 > i {
return 0, fmt.Errorf("int64 value should between %v and %v ", math.MinInt8, math.MaxInt8) return 0, fmt.Errorf("int64 value should between %v and %v ", math.MinInt64, math.MaxInt64)
} }
return int64(i), nil return int64(i), nil
} }
@ -74,7 +74,7 @@ func (i Int) ToInt64() (int64, error) {
// Date : 14:07 2023/5/5 // Date : 14:07 2023/5/5
func (i Int) ToInt() (int, error) { func (i Int) ToInt() (int, error) {
if math.MaxInt < i || math.MinInt > i { if math.MaxInt < i || math.MinInt > i {
return 0, fmt.Errorf("int value should between %v and %v ", math.MinInt8, math.MaxInt8) return 0, fmt.Errorf("int value should between %v and %v ", math.MinInt, math.MaxInt)
} }
return int(i), nil return int(i), nil
} }

89
uint.go Normal file
View File

@ -0,0 +1,89 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-05 14:20
package wrapper
import (
"fmt"
"math"
)
// Uint ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:20 2023/5/5
type Uint uint64
// ToUint8 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:21 2023/5/5
func (ui Uint) ToUint8() (uint8, error) {
if ui > math.MaxUint8 || ui < 0 {
return 0, fmt.Errorf("uint8 should between 0 and %v", math.MaxUint8)
}
return uint8(ui), nil
}
// ToUint16 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:25 2023/5/5
func (ui Uint) ToUint16() (uint16, error) {
if ui > math.MaxUint16 || ui < 0 {
return 0, fmt.Errorf("uint16 should between 0 and %v", math.MaxUint16)
}
return uint16(ui), nil
}
// ToUint32 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:25 2023/5/5
func (ui Uint) ToUint32() (uint32, error) {
if ui > math.MaxUint32 || ui < 0 {
return 0, fmt.Errorf("uint32 should between 0 and %v", math.MaxUint32)
}
return uint32(ui), nil
}
// ToUint64 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:30 2023/5/5
func (ui Uint) ToUint64() (uint64, error) {
if ui > math.MaxUint64 || ui < 0 {
return 0, fmt.Errorf("uint64 should between 0 and %v", math.MaxUint64)
}
return uint64(ui), nil
}
// ToUint ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:31 2023/5/5
func (ui Uint) ToUint() (uint, error) {
if ui > math.MaxUint || ui < 0 {
return 0, fmt.Errorf("uint should between 0 and %v", math.MaxUint)
}
return uint(ui), nil
}
// ToString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:32 2023/5/5
func (ui Uint) ToString() string {
return fmt.Sprintf("%v", ui)
}