hexid

package module
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2025 License: MIT Imports: 13 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtomicGenerator

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

Thread-safe ID generator. Can generate 2^22 (or 4 194 304) locally unique IDs per millisecond, before it wraps around and starts generating duplicates.

func NewAtomicGenerator

func NewAtomicGenerator() AtomicGenerator

Create an atomic ID generator. The generator is thread-safe.

func (*AtomicGenerator) ID

func (g *AtomicGenerator) ID() ID

func (*AtomicGenerator) IDFromTime

func (g *AtomicGenerator) IDFromTime(ts time.Time) ID

type Generator

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

Non-thread-safe ID generator. Can generate 2^22 (or 4 194 304) locally unique IDs per millisecond, before it wraps around and starts generating duplicates.

func NewGenerator

func NewGenerator() Generator

Create an ID generator. The generator is NOT thread-safe.

func (*Generator) ID

func (g *Generator) ID() (id ID)

func (*Generator) IDFromTime

func (g *Generator) IDFromTime(ts time.Time) (id ID)

type ID

type ID uint64

32 bits unix seconds + 32 bits sequence (where first 10 bits are milliseconds)

func Generate

func Generate() ID

Atomically generates the next ID based on current time. Thread-safe.

func HashedID

func HashedID(s ...string) ID

Produces a deterministic ID from string(s). The timestamp part of the resulting ID will be be pointless, but guaranteed to be before 2004-01-10 13:37:04 UTC.

Example
fmt.Println(HashedID("foobar"))
fmt.Println(HashedID("foobaz"))
Output:


06e441e53719e608
f0e4e84ada932890

func HashedIDBytes

func HashedIDBytes(b []byte) ID

Produces a deterministic ID from a byte slice. The timestamp part of the resulting ID will be be pointless, but guaranteed to be before 2004-01-10 13:37:04 UTC.

func IDFromString

func IDFromString(str string) (id ID, err error)

func IDFromTime

func IDFromTime(ts time.Time) ID

Atomically generates the next ID based on provided timestamp. Thread-safe.

func (ID) AppendBinary

func (id ID) AppendBinary(b []byte) ([]byte, error)

AppendBinary implements internal.BinaryAppender.

func (ID) AppendText

func (id ID) AppendText(b []byte) ([]byte, error)

AppendBinary implements internal.TextAppender.

func (ID) IsNil added in v0.1.0

func (id ID) IsNil() bool

func (ID) IsZero added in v0.1.0

func (id ID) IsZero() bool

func (ID) MarshalJSON

func (id ID) MarshalJSON() (b []byte, err error)

MarshalJSON implements json.Marshaler.

func (ID) MarshalText added in v0.2.5

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

MarshalText implements encoding.TextMarshaler.

func (*ID) Scan added in v0.2.0

func (id *ID) Scan(src any) (err error)

Scan implements sql.Scanner.

func (ID) Seq

func (id ID) Seq() uint32

Extracts sequence number (last 32 bits)

func (ID) String

func (id ID) String() string

func (ID) Time

func (id ID) Time() time.Time

Converts ID to time.Time

func (ID) Uint64

func (id ID) Uint64() uint64

func (ID) Unix

func (id ID) Unix() uint32

Unix timestamp in seconds precision

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON implements json.Unmarshaler.

func (*ID) UnmarshalText added in v0.2.5

func (id *ID) UnmarshalText(text []byte) (err error)

UnmarshalText implements encoding.TextUnmarshaler.

func (ID) Value added in v0.2.0

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

Value implements driver.Valuer.

Jump to

Keyboard shortcuts

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