id

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package id generates prefixed, time-ordered identifiers. IDs encode a millisecond timestamp and per-timestamp counter in six big-endian bytes, followed by 14 modulo-biased base62 characters.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ascending

func Ascending(kind string, given ...string) (string, error)

Ascending returns an ascending ID for kind. An absent or empty given value generates a new ID; any other value is prefix-checked and returned verbatim.

func Create

func Create(prefix string, direction Direction, timestamp ...int64) (string, error)

Create uses the process-global generator and an optional explicit timestamp.

func Descending

func Descending(kind string, given ...string) (string, error)

Descending returns a descending ID for kind; given is handled as in Ascending.

func Prefix

func Prefix(kind string) (string, bool)

Prefix returns the short prefix for an entity kind.

func SchemaAccepts

func SchemaAccepts(kind, value string) bool

SchemaAccepts reports whether value carries the prefix for kind.

func Timestamp

func Timestamp(value string) (int64, error)

Timestamp extracts the millisecond timestamp from an ascending ID. It does not invert descending IDs.

Types

type Direction

type Direction string

Direction selects the timestamp byte ordering used by Create.

const (
	AscendingDirection  Direction = "ascending"
	DescendingDirection Direction = "descending"
)

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator owns the monotonic counter and its injectable side effects.

func NewGenerator

func NewGenerator(now func() int64, random io.Reader) *Generator

NewGenerator constructs an independent ID generator. Nil inputs use the wall clock and crypto/rand.

func (*Generator) Ascending

func (g *Generator) Ascending(kind string, given ...string) (string, error)

Ascending generates with this Generator.

func (*Generator) Create

func (g *Generator) Create(prefix string, direction Direction, timestamp ...int64) (string, error)

Create emits an ID from this Generator. State resets only when the current timestamp differs from the previous one; moving the clock backwards is accepted.

func (*Generator) Descending

func (g *Generator) Descending(kind string, given ...string) (string, error)

Descending generates with this Generator.

Jump to

Keyboard shortcuts

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