wrapper/int.go

90 lines
1.9 KiB
Go
Raw Normal View History

2023-05-05 14:08:34 +08:00
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-05 13:56
package wrapper
import (
"fmt"
"math"
)
// Int int类型
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 13:57 2023/5/5
type Int int64
// ToInt8 ...
//
// 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 ", math.MinInt8, math.MaxInt8)
}
return int8(i), nil
}
// ToInt16 ...
//
// 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) {
2023-05-05 14:32:17 +08:00
return 0, fmt.Errorf("int16 value should between %v and %v ", math.MinInt16, math.MaxInt16)
2023-05-05 14:08:34 +08:00
}
return int16(i), nil
}
// ToInt32 ...
//
// 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) {
2023-05-05 14:32:17 +08:00
return 0, fmt.Errorf("int32 value should between %v and %v ", math.MinInt32, math.MaxInt32)
2023-05-05 14:08:34 +08:00
}
return int32(i), nil
}
// ToInt64 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:06 2023/5/5
func (i Int) ToInt64() (int64, error) {
if math.MaxInt64 < i || math.MinInt64 > i {
2023-05-05 14:32:17 +08:00
return 0, fmt.Errorf("int64 value should between %v and %v ", math.MinInt64, math.MaxInt64)
2023-05-05 14:08:34 +08:00
}
return int64(i), nil
}
// ToInt ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:07 2023/5/5
func (i Int) ToInt() (int, error) {
if math.MaxInt < i || math.MinInt > i {
2023-05-05 14:32:17 +08:00
return 0, fmt.Errorf("int value should between %v and %v ", math.MinInt, math.MaxInt)
2023-05-05 14:08:34 +08:00
}
return int(i), nil
}
// ToString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:07 2023/5/5
func (i Int) ToString() string {
return fmt.Sprintf("%v", i)
}