k9

package
v0.85.0-pre.7 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2026 License: BSD-3-Clause Imports: 4 Imported by: 0

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

View Source
const (
	// IDSize is the size, in bytes, of a k9 ID.
	IDSize = 16
)

Variables

View Source
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

func CreationTime(id ID) time.Time

CreationTime converts a k9 ID to a time.Time object representing the creation time in microseconds.

func Serialize

func Serialize(id ID) string

Serialize encodes a k9 ID into a 26-character, human-friendly string representation.

func ToUnixMicro

func ToUnixMicro(id ID) int64

ToUnixMicro converts a k9 ID to a Unix timestamp in microseconds.

Types

type ID

type ID [IDSize]byte

ID is a 16-byte k9 ID.

func FromBytes

func FromBytes(bytes []byte) (ID, error)

FromBytes validates and converts raw 16-byte input into a k9 ID.

func MaxIDAtTime

func MaxIDAtTime(t time.Time) ID

MaxIDAtTime returns the latest possible ID for the given time.

func MinIDAtTime

func MinIDAtTime(t time.Time) ID

MinIDAtTime returns the earliest possible ID for the given time.

func New

func New() (ID, error)

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

func NewMulti(n int) ([]ID, error)

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 Parse

func Parse(str string) (ID, error)

Parse decodes a k9 ID string back into its byte representation.

func (ID) CreationTime

func (id ID) CreationTime() time.Time

CreationTime converts a k9 ID to a time.Time object representing the creation time in microseconds.

func (ID) Serialize

func (id ID) Serialize() string

Serialize encodes a k9 ID into a 26-character, human-friendly string representation.

func (ID) ToUnixMicro

func (id ID) ToUnixMicro() int64

ToUnixMicro converts a k9 ID to a Unix timestamp in microseconds.

Jump to

Keyboard shortcuts

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