-
Notifications
You must be signed in to change notification settings - Fork 0
/
uid.go
148 lines (117 loc) · 3.78 KB
/
uid.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package uid
import (
"crypto/rand"
"strconv"
"strings"
"time"
"github.com/google/uuid"
)
// HumanUid generates a UUID (32 digits) Format: YYYYMMDD-HHMM-SSMM-MMMMNNNRRRRRRRRR
func HumanUid() string {
time.Sleep(1 * time.Nanosecond)
r, _ := rand.Prime(rand.Reader, 64)
id := time.Now().UTC().Format("20060102150405.0000000")
id = strings.ReplaceAll(id, ".", "")
id += r.String()
return id[0:32]
}
// NanoUid generates a UID (23 digits) Format: YYYYMMDD-HHMMSS-MMMMMM-NNN
func NanoUid() string {
time.Sleep(time.Nanosecond) // as its a nanoseconds based ID we need at least a nanosecond between the generations to avoid collisions
r, _ := rand.Prime(rand.Reader, 64)
id := time.Now().UTC().Format("20060102150405.0000000")
id = strings.ReplaceAll(id, ".", "")
id += r.String()
return id[0:23]
}
// MicroUid generates a UID (20 digits) Format: YYYYMMDD-HHMMSS-MMMMMM
func MicroUid() string {
time.Sleep(time.Microsecond) // as its a microseconds based ID we need at least a microsecond between the generations to avoid collisions
r, _ := rand.Prime(rand.Reader, 64)
id := time.Now().UTC().Format("20060102150405.0000000")
id = strings.ReplaceAll(id, ".", "")
id += r.String()
return id[0:20]
}
// SecUid generates UID (14 digits) Format: YYYYMMDD-HHMMSS
func SecUid() string {
time.Sleep(time.Second) // as its a seconds based ID we need at least a second between the generations to avoid collisions
r, _ := rand.Prime(rand.Reader, 64)
id := time.Now().UTC().Format("20060102150405.0000000")
id = strings.ReplaceAll(id, ".", "")
id += r.String()
return id[0:14]
}
func Timestamp() string {
time.Sleep(time.Second) // as its a seconds based ID we need at least a second between the generations to avoid collisions
now := time.Now().UTC().Unix()
return strconv.FormatInt(now, 10)
}
func TimestampMicro() string {
time.Sleep(time.Microsecond) // as its a microseconds based ID we need at least a microsecond between the generations to avoid collisions
now := time.Now().UTC().UnixMicro()
return strconv.FormatInt(now, 10)
}
func TimestampNano() string {
time.Sleep(time.Nanosecond) // as its a nanoseconds based ID we need at least a nanosecond between the generations to avoid collisions
now := time.Now().UTC().UnixNano()
return strconv.FormatInt(now, 10)
}
func Uuid() string {
return strings.ReplaceAll(UuidFormatted(), "-", "")
}
func UuidFormatted() string {
return uuid.New().String()
}
func UuidV1() string {
return strings.ReplaceAll(UuidV1Formatted(), "-", "")
}
func UuidV1Formatted() string {
return uuid.Must(uuid.NewUUID()).String()
}
func UuidV3(namespace string, data []byte) (string, error) {
uid, err := UuidV3Formatted(namespace, data)
if err != nil {
return "", err
}
return strings.ReplaceAll(uid, "-", ""), nil
}
func UuidV3Formatted(namespace string, data []byte) (string, error) {
id, err := uuid.FromBytes([]byte(namespace))
if err != nil {
return "", err
}
return uuid.Must(uuid.NewMD5(id, data), nil).String(), nil
}
func UuidV4() string {
return strings.ReplaceAll(UuidV4Formatted(), "-", "")
}
func UuidV4Formatted() string {
return uuid.New().String()
}
func UuidV5(namespace string, data []byte) (string, error) {
uid, err := UuidV5Formatted(namespace, data)
if err != nil {
return "", err
}
return strings.ReplaceAll(uid, "-", ""), nil
}
func UuidV5Formatted(namespace string, data []byte) (string, error) {
id, err := uuid.FromBytes([]byte(namespace))
if err != nil {
return "", err
}
return uuid.NewSHA1(id, data).String(), nil
}
func UuidV6() string {
return strings.ReplaceAll(UuidV6Formatted(), "-", "")
}
func UuidV6Formatted() string {
return uuid.Must(uuid.NewV6()).String()
}
func UuidV7() string {
return strings.ReplaceAll(UuidV7Formatted(), "-", "")
}
func UuidV7Formatted() string {
return uuid.Must(uuid.NewV7()).String()
}