id

package
v0.16.2 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultUuidIdGenerator = NewUuidIdGenerator()

DefaultUuidIdGenerator is the default UUID v7 generator instance. UUID v7 embeds a timestamp for natural ordering and includes random bits for uniqueness.

View Source
var DefaultXidIdGenerator = NewXidIdGenerator()

DefaultXidIdGenerator is the default XID generator instance.

Functions

func Generate

func Generate() string

Generate creates a new unique identifier using the default XID generator. XID is chosen as the default because it offers the best performance with good uniqueness guarantees. The generated ID is a 20-character string using base32 encoding (0-9, a-v).

Example:

id := Generate()
// Returns something like: "9m4e2mr0ui3e8a215n4g"

func GenerateUuid

func GenerateUuid() string

GenerateUuid creates a new UUID v7 identifier using the default UUID generator. UUID v7 provides time-based ordering and follows RFC 4122 standards. The generated UUID is a 36-character string in the format: xxxxxxxx-xxxx-7xxx-xxxx-xxxxxxxxxxxx

Example:

uuid := GenerateUuid()
// Returns something like: "018f4e42-832a-7123-9abc-def012345678"

Types

type IdGenerator

type IdGenerator interface {
	// Generate creates a new unique identifier as a string.
	// The format and characteristics depend on the specific implementation.
	Generate() string
}

IdGenerator defines the interface for all ID generation strategies. All generators must implement this interface to ensure consistency.

var DefaultSnowflakeIdGenerator IdGenerator

DefaultSnowflakeIdGenerator is the default Snowflake ID generator instance.

func NewRandomIdGenerator

func NewRandomIdGenerator(alphabet string, length int) IdGenerator

NewRandomIdGenerator creates a new random ID generator with custom alphabet and length.

func NewSnowflakeIdGenerator

func NewSnowflakeIdGenerator(nodeId int64) (_ IdGenerator, err error)

NewSnowflakeIdGenerator creates a new Snowflake ID generator for the specified node. The nodeId must be between 0 and 63 (6-bit limit as configured in init). Each node in a distributed system should have a unique nodeId to ensure global uniqueness.

func NewUuidIdGenerator

func NewUuidIdGenerator() IdGenerator

NewUuidIdGenerator creates a new UUID v7 generator instance.

func NewXidIdGenerator

func NewXidIdGenerator() IdGenerator

NewXidIdGenerator creates a new XID generator instance.

Jump to

Keyboard shortcuts

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