Documentation
¶
Overview ¶
Package usid provides microsecond-precision, time-ordered unique identifiers.
USIDs are 64-bit IDs with an embedded timestamp, node ID, and sequence number. They sort chronologically, are URL-safe when encoded, and work well as database primary keys.
Basic usage:
usid.SetNodeID(1) // Call once at startup id := usid.New() // Generate IDs fmt.Println(id) // Crockford Base32 encoded by default
The bit layout is configurable via Epoch, NodeBits, and SeqBits variables.
Index ¶
- Variables
- func Node(id int64) int64deprecated
- func Seq(id int64) int64deprecated
- func SetNodeID(node int64)
- func SetObfuscator(key int64)
- func Timestamp(id int64) time.Timedeprecated
- type Format
- type Generator
- type ID
- func FromBytes(b []byte) (ID, error)
- func FromBytesOrNil(b []byte) ID
- func FromInt64(n int64) ID
- func FromString(s string) (ID, error)
- func FromStringOrNil(s string) ID
- func Must(id ID, err error) ID
- func New() ID
- func Parse(s string) (ID, error)
- func ParseBase58(s string) (ID, error)
- func ParseBase64(s string) (ID, error)
- func ParseCrockford(s string) (ID, error)
- func ParseDecimal(s string) (ID, error)
- func ParseHash(s string) (ID, error)
- func (id ID) Bytes() []byte
- func (id ID) Format(f ...Format) string
- func (id *ID) GobDecode(data []byte) error
- func (id ID) GobEncode() ([]byte, error)
- func (id ID) Hash() [8]byte
- func (id ID) Int64() int64
- func (id ID) IsNil() bool
- func (id ID) MarshalBinary() ([]byte, error)
- func (id ID) MarshalJSON() ([]byte, error)
- func (id ID) MarshalText() ([]byte, error)
- func (id ID) Node() int64
- func (id *ID) Parse(s string) error
- func (id *ID) Scan(src interface{}) error
- func (id ID) Seq() int64
- func (id ID) String() string
- func (id ID) Timestamp() time.Time
- func (id *ID) UnmarshalBinary(data []byte) error
- func (id *ID) UnmarshalJSON(b []byte) error
- func (id *ID) UnmarshalText(b []byte) error
- func (id ID) Value() (driver.Value, error)
- type NullID
- type Obfuscator
Constants ¶
This section is empty.
Variables ¶
var ( // Epoch is the custom epoch in microseconds (default: 2025-12-16). // IDs store time as microseconds since this epoch. Epoch int64 = 1765947799213000 // NodeBits is the number of bits allocated for the node ID (default: 6, max 64 nodes). NodeBits uint8 = 6 // SeqBits is the number of bits allocated for the sequence number (default: 6, max 64 per µs). SeqBits uint8 = 6 // DefaultFormat is the default string encoding format for IDs. DefaultFormat Format = FormatCrockford )
Configuration variables for USID generation. Modify these before generating any IDs if you need custom bit layouts.
var DefaultGenerator = NewGenerator(1)
DefaultGenerator is used by New(). Set via SetNodeID().
Functions ¶
func SetNodeID ¶
func SetNodeID(node int64)
SetNodeID initializes the DefaultGenerator with the given node ID. Call this once at startup before using New().
func SetObfuscator ¶
func SetObfuscator(key int64)
SetObfuscator sets the DefaultObfuscator with the given key. Call once at startup to enable obfuscation.
Types ¶
type Format ¶
type Format string
Format specifies the string encoding format for IDs.
const ( FormatCrockford Format = "crockford" // Crockford Base32, case-insensitive (default) FormatBase58 Format = "base58" // URL-safe, compact FormatBase64 Format = "base64" // Standard base64 encoding FormatHash Format = "hash" // Hexadecimal encoding FormatDecimal Format = "decimal" // Decimal integer string )
Supported ID string formats.
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator produces unique IDs for a specific node. Create with NewGenerator and call Generate to produce IDs.
func NewGenerator ¶
NewGenerator creates a Generator for the given node ID. The node ID must be in the range [0, 2^NodeBits - 1]. Panics if node is out of range.
type ID ¶
type ID int64
ID is a 64-bit microsecond-precision time-ordered identifier.
var Nil ID = 0
Nil is the zero ID, representing an absent or invalid ID.
Omni is the maximum ID value (math.MaxInt64), useful as an upper bound in queries.
func FromBytesOrNil ¶
FromBytesOrNil returns an ID from an 8-byte slice. Returns Nil on error.
func FromString ¶
FromString returns an ID parsed from the input string. Alias for Parse.
func FromStringOrNil ¶
FromStringOrNil returns an ID parsed from the input string. Returns Nil on error.
func New ¶
func New() ID
New generates an ID using the DefaultGenerator. Panics if SetNodeID() hasn't been called.
func ParseBase58 ¶
ParseBase58 parses a base58-encoded string into an ID.
func ParseBase64 ¶
ParseBase64 parses a base64-encoded string into an ID.
func ParseCrockford ¶
ParseCrockford parses a Crockford Base32-encoded string into an ID.
func ParseDecimal ¶
ParseDecimal parses a decimal string into an ID.
func (ID) Format ¶
Format returns the ID as a string in the specified format. If no format is provided, uses DefaultFormat.
func (ID) MarshalBinary ¶
MarshalBinary implements encoding.BinaryMarshaler.
func (ID) MarshalJSON ¶
MarshalJSON implements json.Marshaler
func (ID) MarshalText ¶
MarshalText implements encoding.TextMarshaler
func (*ID) UnmarshalBinary ¶
UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (*ID) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler
func (*ID) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler
type NullID ¶
NullID can be used with the standard sql package to represent an ID value that can be NULL in the database.
func (NullID) MarshalJSON ¶
MarshalJSON marshals the NullID as null or the nested ID as a string.
func (NullID) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*NullID) UnmarshalJSON ¶
UnmarshalJSON unmarshals a NullID.
func (*NullID) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Obfuscator ¶
type Obfuscator struct {
// contains filtered or unexported fields
}
Obfuscator XORs IDs with a key to hide timestamps and sequences in external representations.
var DefaultObfuscator *Obfuscator
DefaultObfuscator, when set, obfuscates all external representations (String, Format, JSON, etc.) while keeping internal values raw. Set this once at startup before generating or parsing IDs.
func NewObfuscator ¶
func NewObfuscator(key int64) *Obfuscator
NewObfuscator creates an obfuscator with the given key. Use a random int64 and keep it secret.
func (*Obfuscator) Deobfuscate ¶
func (o *Obfuscator) Deobfuscate(id ID) ID
Deobfuscate reverses obfuscation (XOR is its own inverse).
func (*Obfuscator) Obfuscate ¶
func (o *Obfuscator) Obfuscate(id ID) ID
Obfuscate XORs the ID with the key.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package base58 provides Base58 encoding and decoding for int64 values.
|
Package base58 provides Base58 encoding and decoding for int64 values. |
|
Package crockford provides Crockford Base32 encoding and decoding for int64 values.
|
Package crockford provides Crockford Base32 encoding and decoding for int64 values. |
|
Package postgres provides PostgreSQL integration for USID.
|
Package postgres provides PostgreSQL integration for USID. |