id

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultRandomIDGeneratorAlphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	DefaultRandomIDGeneratorLength   = 32
)

Variables

View Source
var DefaultUUIDGenerator = NewUUIDGenerator()

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

View Source
var DefaultXIDGenerator = NewXIDGenerator()

DefaultXIDGenerator 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(opts ...RandomIDGeneratorOption) IDGenerator

NewRandomIDGenerator creates a new random ID generator with optional configuration. Defaults to alphanumeric alphabet (62 chars) and length of 32.

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 NewUUIDGenerator

func NewUUIDGenerator() IDGenerator

NewUUIDGenerator creates a new UUID v7 generator instance.

func NewXIDGenerator

func NewXIDGenerator() IDGenerator

NewXIDGenerator creates a new XID generator instance.

type RandomIDGeneratorOption

type RandomIDGeneratorOption func(*randomIDGenerator)

RandomIDGeneratorOption configures a randomIDGenerator instance.

func WithAlphabet

func WithAlphabet(alphabet string) RandomIDGeneratorOption

WithAlphabet sets the character set for random ID generation.

func WithLength

func WithLength(length int) RandomIDGeneratorOption

WithLength sets the length of generated random IDs.

Jump to

Keyboard shortcuts

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