-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
51 lines (43 loc) · 1.08 KB
/
utils.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
package logger
import (
"crypto/md5"
"encoding/hex"
"fmt"
"math/rand"
"strconv"
"strings"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// RndUUID realizes unique uuid based on time ns and random number
// There is no duplication of uuid on a single machine
// If you want to generate non-duplicate uuid on a distributed architecture
// Just add some custom strings in front of rndStr
// Return format: eba1e8cd-0460-4910-49c6-44bdf3cf024d
func RndUUID() string {
s := RndUUIDMd5()
return fmt.Sprintf("%s-%s-%s-%s-%s", s[:8], s[8:12], s[12:16], s[16:20], s[20:])
}
// RndUUIDMd5 make an md5 uuid
func RndUUIDMd5() string {
ns := time.Now().UnixNano()
rndStr := strings.Join([]string{
strconv.FormatInt(ns, 10), strconv.FormatInt(RandInt64(1000, 9999), 10),
}, "")
return Md5(rndStr)
}
// RandInt64 crete a num [m,n]
func RandInt64(min, max int64) int64 {
if min >= max || min == 0 || max == 0 {
return max
}
return rand.Int63n(max-min) + min
}
// Md5 md5 func
func Md5(str string) string {
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}