ulid

package
v0.783.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package ulid decodes a ULID (Universally Unique Lexicographically Sortable Identifier) into its embedded creation timestamp and randomness. A ULID is a 26-character Crockford-base32 string encoding 128 bits — a 48-bit millisecond Unix timestamp followed by 80 bits of randomness — and is widely used by modern backends as a sortable, UUID-sized identifier (the spec's answer to UUIDv4, and a peer of UUIDv7). Like a UUIDv1/v7 or a MongoDB ObjectId, a ULID is NOT opaque: it leaks the **creation time** of whatever it identifies, and its lexicographic sortability aids record enumeration. This completes the identifier-timestamp triad with internal/uuidinfo and internal/objectid. Pure offline transform; no network or device.

Wrap-vs-native judgement

Native. A ULID is Crockford base32 over a fixed 128-bit layout: decode the 26 characters to 16 bytes, then the first 6 bytes are the big-endian millisecond timestamp and the last 10 are the randomness. It is a base-32 decode + a uint48 read — there is nothing to wrap, and no ULID type exists in the Go stdlib. Consistent with the other in-tree identifier decoders.

Verifiable / no confidently-wrong output

Anchored to the reference python-ulid library AND hand-verified: the canonical ULID 01ARZ3NDEKTSV4RRFFQ69G5FAV decodes to 1469922850259 ms (2016-07-30T23:54:10.259Z) — confirmed both by the library and by manually Crockford-decoding its first ten characters. The all-zero ULID decodes to the Unix epoch. Input is rejected unless it is exactly 26 Crockford-base32 characters whose first character is 0-7 (a larger first character would overflow 128 bits — the spec's max ULID is 7ZZ…).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Result

type Result struct {
	ULID          string `json:"ulid"` // canonical uppercase
	TimestampUTC  string `json:"timestamp_utc"`
	UnixMillis    int64  `json:"unix_millis"`
	RandomnessHex string `json:"randomness_hex"` // 80-bit / 10-byte tail
	Note          string `json:"note,omitempty"`
}

Result is the decoded view of a ULID.

func Decode

func Decode(in string) (*Result, error)

Decode parses a 26-character ULID into its millisecond timestamp and 80-bit randomness.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL