-
Notifications
You must be signed in to change notification settings - Fork 3
/
byte.go
executable file
·48 lines (42 loc) · 845 Bytes
/
byte.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Package byteKit
/*
byte <=> uint8
*/
package byteKit
import (
"github.com/richelieu-yang/chimera/v2/src/core/strKit"
"github.com/spf13/cast"
)
func ToByte(obj interface{}) byte {
return ToByteWithDefault(obj, 0)
}
func ToByteWithDefault(obj interface{}, def byte) byte {
b, _ := ToByteWithDefaultE(obj, def)
return b
}
func ToByteWithDefaultE(obj interface{}, def byte) (byte, error) {
switch obj.(type) {
case string:
return stringToByte(obj.(string), def)
default:
if obj == nil {
return def, nil
}
rst, err := cast.ToUint8E(obj)
if err != nil {
return def, err
}
return rst, nil
}
}
func stringToByte(str string, def byte) (byte, error) {
str = strKit.TrimSpace(str)
if strKit.IsEmpty(str) {
return def, nil
}
rst, err := cast.ToUint8E(str)
if err != nil {
return def, err
}
return rst, nil
}