Skip to main content

ID Generation

The id package provides pluggable unique identifier generation. The framework ships three built-in strategies — XID, UUID v7, and random/Nano-style IDs.

API Reference

APIContract
id.IDGeneratorInterface implemented by every built-in generator
IDGenerator.Generate()Returns the next ID as a string; concrete format depends on the generator
id.Generate()Delegates to DefaultXIDGenerator.Generate() and returns a 20-character XID
id.GenerateUUID()Delegates to DefaultUUIDGenerator.Generate() and returns a UUID v7 string
id.DefaultXIDGeneratorPackage-level XID singleton created by NewXIDGenerator()
id.DefaultUUIDGeneratorPackage-level UUID v7 singleton created by NewUUIDGenerator()
id.NewXIDGenerator()Returns an IDGenerator that wraps xid.New().String()
id.NewUUIDGenerator()Returns an IDGenerator that uses uuid.NewV7() and panics if UUID creation fails
id.NewRandomIDGenerator(opts...)Returns a random/Nano-style generator with defaults, then applies options in order
id.RandomIDGeneratorOptionFunction option type used by the random generator constructor
id.WithAlphabet(alphabet)Sets the random generator alphabet
id.WithLength(length)Sets the random generator output length
id.DefaultRandomIDGeneratorAlphabetDefault random alphabet, 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
id.DefaultRandomIDGeneratorLengthDefault random output length, 32

Quick Start

import "github.com/coldsmirk/vef-framework-go/id"

// XID (the default for model primary keys)
xid := id.Generate()
// → "9m4e2mr0ui3e8a215n4g" (20 chars, base32)

// UUID v7 (when RFC 4122 compliance is needed)
uuid := id.GenerateUUID()
// → "018f4e42-832a-7123-9abc-def012345678"

Built-in Generators

XID

XID is the framework's default for model primary keys — best balance of performance and uniqueness.

PropertyValue
Format20-character base32 string (0-9, a-v)
SortableTime-ordered
Globally uniqueMachine ID + counter
PerformanceBest among the three strategies
xid := id.Generate()
// or
xid := id.DefaultXIDGenerator.Generate()
// or
xid = id.NewXIDGenerator().Generate()

UUID v7

Time-based, RFC 4122-compliant UUIDs — use these when integrating with systems that expect canonical UUIDs.

PropertyValue
Format36-character UUID (xxxxxxxx-xxxx-7xxx-xxxx-xxxxxxxxxxxx)
SortableTime-ordered
RFC compliantRFC 4122
uuid := id.GenerateUUID()
// or
uuid := id.DefaultUUIDGenerator.Generate()
// or
uuid = id.NewUUIDGenerator().Generate()

Random / Nano-style

Cryptographically random IDs with a configurable alphabet — useful for short, opaque tokens.

PropertyValue
Default alphabet0-9 a-z A-Z (62 characters; id.DefaultRandomIDGeneratorAlphabet)
Default length32 (id.DefaultRandomIDGeneratorLength)
// Default 32-character alphanumeric token
gen := id.NewRandomIDGenerator()
token := gen.Generate()

// Custom: 16-char numeric-only
gen = id.NewRandomIDGenerator(
id.WithAlphabet("0123456789"),
id.WithLength(16),
)

RandomIDGeneratorOption is the option type used by WithAlphabet(...) and WithLength(...).

Options are applied in the order passed to NewRandomIDGenerator(...). The constructor does not validate custom alphabets or lengths; generation uses go-nanoid/v2 MustGenerate, so an empty alphabet or zero length panics when Generate() is called.

IDGenerator Interface

All built-in generators implement the same interface:

type IDGenerator interface {
Generate() string
}

Pre-built singletons:

id.DefaultXIDGenerator // IDGenerator
id.DefaultUUIDGenerator // IDGenerator

The orm package uses id.Generate() (XID) automatically when inserting records with empty IDs.

When to Use Which

ScenarioRecommendation
General application IDs (primary keys)id.Generate() (XID)
External APIs expecting UUIDsid.GenerateUUID()
Short tokens / invites / shareable linksid.NewRandomIDGenerator(...)
Custom formatImplement IDGenerator yourself