This package provides an implementation of the ULID spec compatible with the GUID/UUID format. It is based on the amazing oklog/ulid package, with the following differences:
- Small and simple API
- Encodes to a standard UUID format and can be used as the UUID type in databases
- More safer and strict as it doesn't allow the generation time to travel backwards
- A faster monotonic RNG that don't allocate memory unnecessarily
- Defaults to text instead of binary when using the sql/driver.Valuer interface
This package requires Go modules.
go get github.com/brunotm/uulid
id, err := uulid.New()
if err != nil {
// handle err
}
fmt.Println(id.String()) // Output: 0178a727-335d-db2d-4671-c5c757718d7c
go test ./...
This repo also provides a tool to generate and parse UULIDs at the command line.
go get -v github.com/brunotm/ulid/cmd/uulid
Usage:
Usage of uulid:
-local
when parsing, show local time instead of UTC
-p string
parse the given uulid
- 48 bits
- UNIX-time in milliseconds
- Won't run out of space till the year 10889 AD
- 80 bits
Monotonicity
within the same millisecond
The components are encoded as 16 octets. Each component is encoded with the Most Significant Byte first (network byte order).
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 32_bit_uint_time_high |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 16_bit_uint_time_low | 16_bit_uint_random |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 32_bit_uint_random |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 32_bit_uint_random |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
This package encodes the ULID into the standard UUID format. (The ULID spec uses Crockford's Base32)
UULID: 0178a73c-3cc0-71ab-74b4-cc6c7190deae
0178a73c3cc0 71ab74b4cc6c7190deae
|------------| |--------------------|
Timestamp Entropy
12 chars 20 chars
48bits 80bits
base32 base32