Documentation
¶
Index ¶
Constants ¶
const ( DefaultRandomIDGeneratorAlphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" DefaultRandomIDGeneratorLength = 32 )
Variables ¶
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.
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.
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 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.