-
Notifications
You must be signed in to change notification settings - Fork 1
/
ulid.go
56 lines (48 loc) · 1.21 KB
/
ulid.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
package report
import (
"math/rand"
"sync"
"time"
"github.com/oklog/ulid"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
var entropyPool = sync.Pool{
New: func() interface{} {
return rand.New(rand.NewSource(time.Now().UnixNano()))
},
}
// ULID creates a Universally Unique Lexicographically Sortable Identifier
//
// A ULID is:
// * compatible with UUID/GUID
// * Lexicographically sortable
// * Canonically encoded as a 26 character string, as opposed
// to the 36 character ID
// * Uses Crockford's base32 for better efficiency and readability
// (5 bits per character)
// * Case insensitive
// * No special characters (URL safe)
//
func createULID() string {
entropy := entropyPool.Get().(*rand.Rand)
defer func() {
entropyPool.Put(entropy)
}()
u, err := ulid.New(ulid.Timestamp(time.Now()), entropy)
if err != nil {
return randString(26)
}
return u.String()
}
// Crockford's Base32 is used as shown. This alphabet excludes the
// letters I, L, O, and U to avoid confusion and abuse.
const randStringLetters = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
func randString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = randStringLetters[rand.Intn(len(randStringLetters))]
}
return string(b)
}