-
Notifications
You must be signed in to change notification settings - Fork 3
/
ulid.go
33 lines (27 loc) · 944 Bytes
/
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
package idKit
import (
"github.com/oklog/ulid/v2"
"io"
)
// NewULID ULID(不建议用作分布式唯一id,小概率会重复)
/*
PS:
(1) Format: tttttttttteeeeeeeeeeeeeeee where t is time and e is entropy.(时间+随机数)
(2) If you just want to generate a ULID and don't (yet) care about details like performance, cryptographic security, etc., use the ulid.Make helper function.
This function calls time.Now to get a timestamp, and uses a source of entropy which is process-global, pseudo-random, and monotonic.
@return 长度: 26(即 ulid.EncodedSize)
e.g.
() => "01GMSRXRWJPYSQQZ5Z6T832CSZ"
*/
func NewULID() string {
return ulid.Make().String()
}
func NewCustomizedULID(ms uint64, entropy io.Reader) (string, error) {
//entropy := rand.New(rand.NewSource(time.Now().UnixNano()))
//ms := ulid.Timestamp(time.Now())
id, err := ulid.New(ms, entropy)
if err != nil {
return "", err
}
return id.String(), nil
}