unique

package module
v0.0.0-...-4161016 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2021 License: Apache-2.0 Imports: 4 Imported by: 2

README

unique

This package implements generation and serialization of ULID-based unique identifiers.

FAQ

Why ULID?
  1. The ULID specification clearly outlines ULID's advantages over traditional UUID/GUID.

  2. We generate IDs in distributed environments, some of which don't have network access. This rules out centralized IDs and we must rely on low probability of collision.

  3. ULIDs are k-sortable in both binary and string form, case-sensitive, URL-friendly, and reasonably compact.

Why not just use oklog/ulid directly?

We use this under the hood. The following considerations lead us to maintaining our own wrapper or fork.

  1. We prefer controlling a stable interface for something as fundamental as IDs.

  2. We want to tweak the interface to better suit our code bases. Specifically,

    • We prefer text marshalling to default to strict validation. We're willing to spend a few extra cycles on conversion.
    • ulid.ULID requires conversion from the 48-bit timestamp to time.Time. The latter is far more common in our code.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ID

type ID [16]byte

ID is 128-bit sortable unique ID.

See specification at https://github.com/ulid/spec

func NewID

func NewID() ID

NewID creates a new unique ID.

func (ID) Bytes

func (id ID) Bytes() []byte

Bytes returns a byte slice representation of an ID.

func (ID) Entropy

func (id ID) Entropy() []byte

Entropy returns the entropy component of an ID.

func (ID) IsZero

func (id ID) IsZero() bool

IsZero reports whether an ID is all zeroes, such as when it is uninitialized.

func (ID) MarshalBinary

func (id ID) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (ID) MarshalText

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

MarshalText implements the encoding.TextMarshaler interface.

func (*ID) MustSetTime

func (id *ID) MustSetTime(t time.Time)

MustSetTime is a convenience function equivalent to SetTime that panics on failure instead of returning an error.

func (*ID) SetEntropy

func (id *ID) SetEntropy(entropy []byte)

SetEntropy sets an ID's entropy. Excess entropy is truncated to the first 10 bytes. Insufficient entropy will be padded to 10 bytes with leading zeroes.

func (*ID) SetTime

func (id *ID) SetTime(t time.Time) error

SetTime sets an ID's time component.

If time is known to be between the years [1970, 10889), the error may be safely ignored; see MustSetTime.

func (ID) String

func (id ID) String() string

String returns the ID's canonical string form.

func (ID) Time

func (id ID) Time() time.Time

Time returns the time component of an ID.

func (*ID) UnmarshalBinary

func (id *ID) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

func (*ID) UnmarshalText

func (id *ID) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (ID) WithEntropy

func (id ID) WithEntropy(entropy []byte) ID

WithEntropy is a convenience function equivalent to SetEntropy which returns a copy of an ID with the given entropy.

func (ID) WithTime

func (id ID) WithTime(t time.Time) ID

WithTime is a convenience function equivalent to MustSetTime which returns a copy of an ID with the given time component.

Jump to

Keyboard shortcuts

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