uid

package
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateShortID

func GenerateShortID() string

GenerateShortID creates a new 11-character lowercase short ID. It encodes the current microsecond timestamp in Crockford Base32. Thread-safe via mutex to prevent duplicate IDs under concurrency.

Business Logic:

  • Uses time.Now().UnixMicro() as the uniqueness base.
  • Maintains package-level lastTimestamp and counter across calls.
  • Counter increments when the timestamp equals the previous call; resets to 0 when the timestamp changes.
  • The counter is 4 bits (0-15), giving 16 unique IDs per timestamp tick.
  • On Windows the system timer resolution is ~1 ms, so UnixMicro returns the same value for every call within the same millisecond.
  • Once the counter exceeds 15 it would overflow and wrap back to 0, producing the exact same composite value and therefore a duplicate ID.
  • Sleeping 1 ms guarantees the timestamp advances to the next tick.
  • Packs timestamp and counter into a single int64: (ts << 4) | counter.
  • The timestamp occupies the high bits; the counter occupies the low 4 bits.
  • Encodes the composite value using Crockford Base32 alphabet.
  • Converts the result to lowercase.
  • Returns an 11-character string (e.g. "sa4rc789wxg").

func IsShortID

func IsShortID(id string) bool

IsShortID checks whether the given string matches short ID lengths (11 or 21 chars).

func NormalizeID

func NormalizeID(id string) string

NormalizeID normalizes an ID to lowercase for consistent lookups.

Types

This section is empty.

Jump to

Keyboard shortcuts

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