-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
46 lines (41 loc) · 951 Bytes
/
util.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
package util
import (
"reflect"
"strconv"
"github.com/alkaid/behavior/logger"
gonanoid "github.com/matoous/go-nanoid"
"go.uber.org/zap"
)
func Float(origin any) (out float64, ok bool) {
rv := reflect.ValueOf(origin)
rt := rv.Type()
switch rt.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
out = float64(rv.Int())
ok = true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
out = float64(rv.Uint())
ok = true
case reflect.Float32, reflect.Float64:
out = rv.Float()
ok = true
case reflect.String:
tmp, err := strconv.Atoi(rv.String())
if err == nil {
out = float64(tmp)
ok = true
}
}
return out, ok
}
const defaultNanoIDLen = 16
// NanoID 随机唯一ID like UUID
//
// @return string
func NanoID() string {
id, err := gonanoid.ID(defaultNanoIDLen)
if err != nil {
logger.Log.Error("", zap.Error(err))
}
return id
}