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.