ulid

package
v2.1.3 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ulid provides ULID generation helpers (Universally Unique Lexicographically Sortable Identifiers).

ULIDs encode time for lexical ordering, but this package does not make ULIDs monotonic within the same millisecond. Generators are concurrency-safe iff the injected RNG is safe.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidULID = errors.New("randutil: invalid ulid")

ErrInvalidULID is returned when parsing fails.

Functions

This section is empty.

Types

type Generator

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

Generator builds ULIDs using a core RNG.

Concurrency: safe for concurrent use if the underlying RNG is safe.

func Default

func Default() *Generator

Default returns the package-wide default generator.

func New

func New(rng rng) *Generator

New returns a ULID Generator. If rng is nil, crypto/rand is used.

func NewWithClock

func NewWithClock(rng rng, now func() time.Time) *Generator

NewWithClock returns a ULID Generator bound to rng and clock. If rng is nil, crypto/rand is used. If now is nil, time.Now is used.

func NewWithSource

func NewWithSource(src core.Source) *Generator

NewWithSource returns a ULID Generator bound to src.

func (*Generator) ULID

func (g *Generator) ULID() (ULID, error)

ULID returns a ULID based on the current time and random data.

Example
gen := Default()

id, err := gen.ULID()
if err != nil {
	fmt.Println("error")
	return
}
parsed, err := Parse(id.String())
if err != nil {
	fmt.Println("error")
	return
}

fmt.Println(len(id))
fmt.Println(parsed == id)
Output:
26
true

type ULID

type ULID string

ULID is the canonical string form of a ULID.

func ID

func ID() (ULID, error)

ID returns a new ULID from the default generator.

func Parse

func Parse(s string) (ULID, error)

Parse validates s and returns a canonical upper-case ULID.

func (ULID) String

func (u ULID) String() string

String returns the ULID string.

Jump to

Keyboard shortcuts

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