uuid

package
v0.0.0-...-d267593 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package uuid provides Forge's default UUIDv7 identifier.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalid = errors.New("invalid UUIDv7")

ErrInvalid is returned for text or bytes that are not a UUIDv7.

View Source
var ErrSequenceExhausted = errors.New("UUIDv7 sequence exhausted for millisecond")

ErrSequenceExhausted is returned when more UUIDs are requested within one millisecond than the monotonic sequence can number.

Functions

This section is empty.

Types

type Generator

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

Generator creates UUIDv7 values that increase monotonically, also within one millisecond. It is safe for concurrent use.

func NewGenerator

func NewGenerator(clock func() time.Time, random io.Reader) (*Generator, error)

NewGenerator returns a Generator that reads the time from clock and random bytes from random; both are required.

func (*Generator) New

func (g *Generator) New() (UUID, error)

New returns the next UUIDv7: the generator's clock in milliseconds, with a sequence that increases within the same millisecond or when the clock does not move forward.

type UUID

type UUID [16]byte

UUID is a 128-bit RFC 9562 UUIDv7 value.

func MustNew

func MustNew() UUID

MustNew is New, and panics when the system entropy source fails. Use it in tests and fixtures.

func New

func New() (UUID, error)

New returns a new UUIDv7 from the default generator, which uses the system clock and crypto/rand.

func NewV7

func NewV7() (UUID, error)

NewV7 is New.

func Parse

func Parse(value string) (UUID, error)

Parse parses the canonical 36-character text of a UUIDv7; any other UUID version or variant is ErrInvalid.

func (UUID) Compare

func (id UUID) Compare(other UUID) int

Compare compares the UUIDs bytewise: because the timestamp comes first, it orders UUIDs by creation time. It returns -1, 0 or 1.

func (UUID) MarshalJSON

func (id UUID) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler with the canonical text as a JSON string.

func (UUID) MarshalText

func (id UUID) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler with the canonical text.

func (*UUID) Scan

func (id *UUID) Scan(value any) error

Scan implements sql.Scanner. It accepts the canonical text, or 16 raw bytes, of a UUIDv7 and rejects any other value.

func (UUID) String

func (id UUID) String() string

func (UUID) Time

func (id UUID) Time() time.Time

Time returns the creation time in the UUID's millisecond timestamp, in UTC.

func (*UUID) UnmarshalJSON

func (id *UUID) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler; it accepts only a JSON string holding a UUIDv7.

func (*UUID) UnmarshalText

func (id *UUID) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler; it accepts only a UUIDv7.

func (UUID) Value

func (id UUID) Value() (driver.Value, error)

Value implements driver.Valuer; the database receives the canonical text.

func (UUID) Variant

func (id UUID) Variant() byte

Variant returns 2 for the RFC 4122/RFC 9562 variant.

func (UUID) Version

func (id UUID) Version() byte

Version returns the UUID version; it is 7 for every value this package creates.

Jump to

Keyboard shortcuts

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