-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
119 lines (100 loc) · 2.05 KB
/
check.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package rose
import (
"fmt"
"runtime"
"strconv"
"strings"
)
func MustBool(s string, defVal ...bool) bool {
if s == "" {
return getBoolDefault(defVal...)
}
b, err := strconv.ParseBool(strings.TrimSpace(s))
if err != nil {
return getBoolDefault(defVal...)
}
return b
}
func getBoolDefault(defVal ...bool) bool {
if len(defVal) > 0 {
return defVal[0]
}
return false
}
// ************
func getFloatDefault(defVals ...float64) float64 {
if len(defVals) > 0 {
return defVals[0]
}
return 0.0
}
func mustFloat(s string, defVals ...float64) float64 {
if s == "" {
return getFloatDefault()
}
f, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
if err != nil {
return getFloatDefault()
}
return f
}
func MustFloat(inter interface{}, defaultVals ...float64) float64 {
switch v := inter.(type) {
case float64:
return v
case string:
return mustFloat(v, defaultVals...)
case int64:
return float64(v)
case float32:
return float64(v)
default:
return getFloatDefault(defaultVals...)
}
}
// MustInt 字符串转int
func MustInt(s string, defVal ...int) int {
getDefault := func() int {
if len(defVal) > 0 {
return defVal[0]
}
return 0
}
if s == "" {
return getDefault()
}
i, err := strconv.Atoi(strings.TrimSpace(s))
if err != nil {
msg := "goutils MustInt strconv.Atoi error:" + err.Error()
// 加上文件调用和行号
_, callerFile, line, ok := runtime.Caller(1)
if ok {
msg += fmt.Sprintf("file:%s,line:%d", callerFile, line)
}
return getDefault()
}
return i
}
// MustInt64 字符串转int64
func MustInt64(s string, defVal ...int64) int64 {
getDefault := func() int64 {
if len(defVal) > 0 {
return defVal[0]
}
return 0
}
if s == "" {
return getDefault()
}
i, err := strconv.ParseInt(strings.TrimSpace(s), 10, 64)
if err != nil {
msg := "goutils MustInt64 strconv.ParseInt error:" + err.Error()
// 加上文件调用和行号
_, callerFile, line, ok := runtime.Caller(1)
if ok {
msg += fmt.Sprintf("file:%s,line:%d", callerFile, line)
}
return getDefault()
}
return i
}