94 lines
1.9 KiB
Go
94 lines
1.9 KiB
Go
// 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", uint8(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", uint16(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", uint32(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", uint64(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", uint(math.MaxUint))
|
|
}
|
|
return uint(ui), nil
|
|
}
|
|
|
|
// ToString ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 14:32 2023/5/5
|
|
func (ui Uint) ToString() (string, error) {
|
|
uint64Val, err := ui.ToUint64()
|
|
if nil != err {
|
|
return "", err
|
|
}
|
|
return fmt.Sprintf("%v", uint64Val), nil
|
|
}
|