Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultUuidIdGenerator = NewUuidIdGenerator()
DefaultUuidIdGenerator is the default UUID v7 generator instance. UUID v7 provides time-based ordering and follows RFC 4122 standards. It produces 36-character strings in the format: xxxxxxxx-xxxx-7xxx-xxxx-xxxxxxxxxxxx.
var DefaultXidIdGenerator = NewXidIdGenerator()
DefaultXidIdGenerator is the default XID generator instance. XID generates globally unique IDs with good performance characteristics. It produces 20-character strings using base32 encoding (0-9, a-v).
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. It's initialized during package init with node ID from NODE_ID environment variable (default: 0). Snowflake IDs are 64-bit integers encoded as Base36 strings for compactness.
func NewRandomIdGenerator ¶
func NewRandomIdGenerator(alphabet string, length int) IdGenerator
NewRandomIdGenerator creates a new random ID generator with custom alphabet and length. This generator is useful when you need specific requirements:
- Custom character sets (e.g., only numbers, only letters)
- Specific length requirements
- Avoiding confusing characters (e.g., 0, O, I, l)
- Custom encoding schemes
Parameters:
- alphabet: The character set to use (must not be empty)
- length: The length of generated IDs (must be positive)
Common alphabets:
- Numbers only: "0123456789"
- Hex: "0123456789abcdef"
- Base62: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
- URL-safe: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"
Example:
// Numeric IDs only
numGen := NewRandomIdGenerator("0123456789", 12)
numId := numGen.Generate() // Returns something like "847392759284"
// Short hex IDs
hexGen := NewRandomIdGenerator("0123456789abcdef", 8)
hexId := hexGen.Generate() // Returns something like "a3f7b2e9"
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.
Example:
gen, err := NewSnowflakeIdGenerator(1)
if err != nil {
log.Fatal(err)
}
id := gen.Generate()
func NewUuidIdGenerator ¶
func NewUuidIdGenerator() IdGenerator
NewUuidIdGenerator creates a new UUID v7 generator instance. UUID v7 is recommended when you need:
- Standards compliance (RFC 4122)
- Time-based sorting
- Compatibility with existing UUID-based systems
- Database-friendly format
Note: UUID v7 is slightly slower than XID but provides better standards compliance.
Example:
gen := NewUuidIdGenerator() id := gen.Generate() // Returns something like "018f4e42-832a-7123-9abc-def012345678"
func NewXidIdGenerator ¶
func NewXidIdGenerator() IdGenerator
NewXidIdGenerator creates a new XID generator instance. XID is recommended for high-performance scenarios where you need:
- Fast generation (best performance among all generators)
- Compact representation (20 characters)
- Time-based sorting
- No coordination between nodes
Example:
gen := NewXidIdGenerator() id := gen.Generate() // Returns something like "9m4e2mr0ui3e8a215n4g"