-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
71 lines (65 loc) · 1.24 KB
/
type.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package utils
import (
"errors"
"strconv"
)
//数据类型转换
var ErrNil = errors.New("nil or type error")
func Int(reply interface{}) (int, error) {
switch reply := reply.(type) {
case int64:
x := int(reply)
if int64(x) != reply {
return 0, strconv.ErrRange
}
return x, nil
case int:
x := int(reply)
return x, nil
case []byte:
n, err := strconv.ParseInt(string(reply), 10, 0)
return int(n), err
case string:
n, err := strconv.Atoi(reply)
return n, err
case nil:
return 0, ErrNil
}
return 0, ErrNil
}
func Int64(reply interface{}) (int64, error) {
switch reply := reply.(type) {
case int64:
return reply, nil
case []byte:
n, err := strconv.ParseInt(string(reply), 10, 64)
return n, err
case nil:
return 0, ErrNil
}
return 0, ErrNil
}
func Float64(reply interface{}) (float64, error) {
switch reply := reply.(type) {
case []byte:
n, err := strconv.ParseFloat(string(reply), 64)
return n, err
case nil:
return 0, ErrNil
}
return 0, ErrNil
}
func String(reply interface{}) (string, error) {
switch reply := reply.(type) {
case []byte:
return string(reply), nil
case string:
return reply, nil
case int:
n := strconv.Itoa(reply)
return n, nil
case nil:
return "", ErrNil
}
return "", ErrNil
}