Documentation
ΒΆ
Index ΒΆ
- func SetValuerType(typ valuer.Type) error
- type AtomicGenerator
- type Generator
- type ID
- func (id ID) AppendBinary(b []byte) ([]byte, error)
- func (id ID) AppendText(b []byte) ([]byte, error)
- func (id ID) Bytes() []byte
- func (id ID) Entropy() uint32
- func (id ID) Hashed() bool
- func (id ID) Int64() int64
- func (id ID) IsNil() bool
- func (id ID) IsZero() bool
- func (id ID) MarshalJSON() (b []byte, err error)
- func (id ID) MarshalText() (text []byte, err error)
- func (id ID) Millis() uint16
- func (id ID) Node() uint8
- func (id *ID) Scan(src any) (err error)
- func (id ID) Seq() uint16
- func (id ID) String() string
- func (id ID) Time() time.Time
- func (id ID) Uint64() uint64
- func (id ID) Unix() uint32
- func (id *ID) UnmarshalJSON(b []byte) (err error)
- func (id *ID) UnmarshalText(text []byte) (err error)
- func (id ID) Value() (driver.Value, error)
Examples ΒΆ
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func SetValuerType ΒΆ added in v0.3.0
Types ΒΆ
type AtomicGenerator ΒΆ
type AtomicGenerator struct {
// contains filtered or unexported fields
}
Thread-safe ID generator. Can generate up to 2^15 (32,768) locally unique IDs per millisecond per node.
func NewAtomicGenerator ΒΆ
func NewAtomicGenerator(node ...uint8) (g AtomicGenerator, err error)
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 up to 2^15 (32,768) locally unique IDs per millisecond per node.
Example ΒΆ
g, err := NewGenerator()
if err != nil {
panic(err)
}
// Ensure a deterministic sequence in this example
g.seq = 1
ts := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
for range 4 {
id := g.IDFromTime(ts)
fmt.Println(id)
}
Output: 58c1fa4a4a00ca4f c7af08e7eeda149e 369c178593b35eed a5892623388ca93c
func NewGenerator ΒΆ
Create an ID generator. The generator is NOT thread-safe.
type ID ΒΆ
type ID uint64
func Generate ΒΆ
func Generate() ID
Atomically generates the next ID based on current time. Thread-safe.
func HashedID ΒΆ
HashedID produces a deterministic 63-bit ID from one or more strings. The resulting ID is based on an FNV-1a hash and always has node ID = 0. The timestamp portion is meaningless but guaranteed not to collide with time-based IDs, since those always have node β₯ 1.
Example ΒΆ
a := HashedID("foobar")
b := HashedID("foobaz")
fmt.Println(a.Hashed(), a)
fmt.Println(b.Hashed(), b)
Output: true 45ecc9eb54b12098 true 951ba2f26ae6feb0
func HashedIDBytes ΒΆ
HashedIDBytes produces a deterministic 63-bit ID from a byte slice. The resulting ID always has node ID = 0.
func IDFromEntropy ΒΆ added in v1.2.0
Reassambles an ID from `(ID).Unix()` and `(ID).Entropy()`.
Example ΒΆ
id1, _ := IDFromString("24a3b3372e1a0a50")
id2 := IDFromEntropy(id1.Unix(), id1.Entropy())
fmt.Println(id1)
fmt.Println(id2)
Output: 24a3b3372e1a0a50 24a3b3372e1a0a50
func IDFromString ΒΆ
func IDFromTime ΒΆ
Atomically generates the next ID based on provided timestamp. Thread-safe.
func (ID) AppendBinary ΒΆ
AppendBinary implements internal.BinaryAppender.
func (ID) AppendText ΒΆ
AppendBinary implements internal.TextAppender.
func (ID) Entropy ΒΆ added in v1.1.0
Entropy returns everything after the Unix timestamp seconds (milliseconds + node + sequence)
func (ID) MarshalJSON ΒΆ
MarshalJSON implements json.Marshaler.
func (ID) MarshalText ΒΆ added in v0.2.5
MarshalText implements encoding.TextMarshaler.
func (*ID) UnmarshalJSON ΒΆ
UnmarshalJSON implements json.Unmarshaler.
func (*ID) UnmarshalText ΒΆ added in v0.2.5
UnmarshalText implements encoding.TextUnmarshaler.