Documentation
¶
Overview ¶
Package k9 generates 16-byte k-sortable IDs, with human-friendly string encoding. Leading 52 bits are a Unix timestamp in 100-microsecond ticks (big endian). Trailing 76 bits are cryptographically random. String representation is 26 characters (base32-encoded, with no padding, and with bytes 0-6 XOR'd with bytes 7-13, respectively, to make it easier for human eyes to differentiate IDs). Only the byte representation is k-sortable, not the string representation.
Index ¶
Constants ¶
const (
// IDSize is the size, in bytes, of a k9 ID.
IDSize = 16
)
Variables ¶
var ( // ErrInvalidCount indicates an invalid count argument. ErrInvalidCount = errors.New("k9: invalid count") // ErrInvalidIDLength indicates an invalid raw ID byte length. ErrInvalidIDLength = errors.New("k9: invalid ID length") // ErrInvalidTextLength indicates an invalid encoded ID text length. ErrInvalidTextLength = errors.New("k9: invalid encoded ID length") )
Functions ¶
func CreationTime ¶
CreationTime converts a k9 ID to a time.Time object representing the creation time in microseconds.
func Serialize ¶
Serialize encodes a k9 ID into a 26-character, human-friendly string representation.
func ToUnixMicro ¶
ToUnixMicro converts a k9 ID to a Unix timestamp in microseconds.
Types ¶
type ID ¶
ID is a 16-byte k9 ID.
func MaxIDAtTime ¶
MaxIDAtTime returns the latest possible ID for the given time.
func MinIDAtTime ¶
MinIDAtTime returns the earliest possible ID for the given time.
func New ¶
New generates a new 16-byte k9 ID, beginning with the unix timestamp at creation (in 100-microsecond ticks, big-endian), followed by 76 bits of cryptographic randomness.
func NewMulti ¶
NewMulti generates n new IDs with unified error handling. Returns ErrInvalidCount when n is negative or too large. IDs produced by a single call share one timestamp tick.
func (ID) CreationTime ¶
CreationTime converts a k9 ID to a time.Time object representing the creation time in microseconds.
func (ID) Serialize ¶
Serialize encodes a k9 ID into a 26-character, human-friendly string representation.
func (ID) ToUnixMicro ¶
ToUnixMicro converts a k9 ID to a Unix timestamp in microseconds.