优化uint类型包装
This commit is contained in:
parent
94707fcb3e
commit
9ca35bf51b
85
uint.go
85
uint.go
@ -24,11 +24,17 @@ type Uint uint64
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 14:21 2023/5/5
|
// Date : 14:21 2023/5/5
|
||||||
func (ui Uint) ToUint8() (uint8, error) {
|
func (ui Uint) ToUint8() Uint8Result {
|
||||||
if ui > math.MaxUint8 || ui < 0 {
|
res := Uint8Result{
|
||||||
return 0, fmt.Errorf("uint8 should between 0 and %v", uint8(math.MaxUint8))
|
Value: 0,
|
||||||
|
Err: nil,
|
||||||
}
|
}
|
||||||
return uint8(ui), nil
|
if ui > math.MaxUint8 || ui < 0 {
|
||||||
|
res.Err = fmt.Errorf("uint8 should between 0 and %v", uint8(math.MaxUint8))
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
res.Value = uint8(ui)
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToUint16 ...
|
// ToUint16 ...
|
||||||
@ -36,11 +42,17 @@ func (ui Uint) ToUint8() (uint8, error) {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 14:25 2023/5/5
|
// Date : 14:25 2023/5/5
|
||||||
func (ui Uint) ToUint16() (uint16, error) {
|
func (ui Uint) ToUint16() Uint16Result {
|
||||||
if ui > math.MaxUint16 || ui < 0 {
|
res := Uint16Result{
|
||||||
return 0, fmt.Errorf("uint16 should between 0 and %v", uint16(math.MaxUint16))
|
Value: 0,
|
||||||
|
Err: nil,
|
||||||
}
|
}
|
||||||
return uint16(ui), nil
|
if ui > math.MaxUint16 || ui < 0 {
|
||||||
|
res.Err = fmt.Errorf("uint16 should between 0 and %v", uint16(math.MaxUint16))
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
res.Value = uint16(ui)
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToUint32 ...
|
// ToUint32 ...
|
||||||
@ -48,11 +60,17 @@ func (ui Uint) ToUint16() (uint16, error) {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 14:25 2023/5/5
|
// Date : 14:25 2023/5/5
|
||||||
func (ui Uint) ToUint32() (uint32, error) {
|
func (ui Uint) ToUint32() Uint32Result {
|
||||||
if ui > math.MaxUint32 || ui < 0 {
|
res := Uint32Result{
|
||||||
return 0, fmt.Errorf("uint32 should between 0 and %v", uint32(math.MaxUint32))
|
Value: 0,
|
||||||
|
Err: nil,
|
||||||
}
|
}
|
||||||
return uint32(ui), nil
|
if ui > math.MaxUint32 || ui < 0 {
|
||||||
|
res.Err = fmt.Errorf("uint32 should between 0 and %v", uint32(math.MaxUint32))
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
res.Value = uint32(ui)
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToUint64 ...
|
// ToUint64 ...
|
||||||
@ -60,11 +78,16 @@ func (ui Uint) ToUint32() (uint32, error) {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 14:30 2023/5/5
|
// Date : 14:30 2023/5/5
|
||||||
func (ui Uint) ToUint64() (uint64, error) {
|
func (ui Uint) ToUint64() Uint64Result {
|
||||||
if ui > math.MaxUint64 || ui < 0 {
|
res := Uint64Result{
|
||||||
return 0, fmt.Errorf("uint64 should between 0 and %v", uint64(math.MaxUint64))
|
Value: 0,
|
||||||
|
Err: nil,
|
||||||
}
|
}
|
||||||
return uint64(ui), nil
|
if ui > math.MaxUint64 || ui < 0 {
|
||||||
|
res.Err = fmt.Errorf("uint64 should between 0 and %v", uint64(math.MaxUint64))
|
||||||
|
}
|
||||||
|
res.Value = uint64(ui)
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToUint ...
|
// ToUint ...
|
||||||
@ -72,11 +95,17 @@ func (ui Uint) ToUint64() (uint64, error) {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 14:31 2023/5/5
|
// Date : 14:31 2023/5/5
|
||||||
func (ui Uint) ToUint() (uint, error) {
|
func (ui Uint) ToUint() UintResult {
|
||||||
if ui > math.MaxUint || ui < 0 {
|
res := UintResult{
|
||||||
return 0, fmt.Errorf("uint should between 0 and %v", uint(math.MaxUint))
|
Value: 0,
|
||||||
|
Err: nil,
|
||||||
}
|
}
|
||||||
return uint(ui), nil
|
if ui > math.MaxUint || ui < 0 {
|
||||||
|
res.Err = fmt.Errorf("uint should between 0 and %v", uint(math.MaxUint))
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
res.Value = uint(ui)
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToString ...
|
// ToString ...
|
||||||
@ -84,10 +113,16 @@ func (ui Uint) ToUint() (uint, error) {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 14:32 2023/5/5
|
// Date : 14:32 2023/5/5
|
||||||
func (ui Uint) ToString() (string, error) {
|
func (ui Uint) ToString() StringResult {
|
||||||
uint64Val, err := ui.ToUint64()
|
uint64Val := ui.ToUint64()
|
||||||
if nil != err {
|
if nil != uint64Val.Err {
|
||||||
return "", err
|
return StringResult{
|
||||||
|
Value: "",
|
||||||
|
Err: uint64Val.Err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return StringResult{
|
||||||
|
Value: fmt.Sprintf("%v", uint64Val),
|
||||||
|
Err: nil,
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%v", uint64Val), nil
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user