KUID is a Go package that provides a compressed universally unique identifier implementation. It offers a more compact representation of UUIDs by using base62 encoding, making them URL-safe and more space-efficient while maintaining compatibility with standard UUIDs.
- Generate random KUIDs
- Convert between UUID and KUID formats
- Base62 encoding for URL-safe identifiers
- Compact 22-character representation (compared to 36 characters for UUID)
- Binary encoding/decoding support
- Thread-safe implementation
go get github.com/alphabatem/kuid
kuid, err := kuid.NewKUID()
if err != nil {
log.Fatal(err)
}
fmt.Println(kuid.String()) // Outputs a 22-character base62 string
uuid := "550e8400-e29b-41d4-a716-446655440000"
kuid, err := kuid.FromUUID(uuid)
if err != nil {
log.Fatal(err)
}
fmt.Println(kuid.String())
kuid, _ := kuid.NewKUID()
uuid := kuid.ToUUID()
fmt.Println(uuid) // Standard UUID format
str := "4B1FkY2xHJRF6PTYR8Xj2Z"
kuid, err := kuid.FromString(str)
if err != nil {
log.Fatal(err)
}
kuid1, _ := kuid.NewKUID()
kuid2, _ := kuid.NewKUID()
if kuid1.Equal(kuid2) {
fmt.Println("KUIDs are equal")
}
KUID internally stores the identifier as two uint64 values (most significant bits and least significant bits). The string representation uses base62 encoding (0-9, A-Z, a-z) to achieve a compact 22-character format:
- Each uint64 is encoded into 11 characters
- Total length is 22 characters
- Maintains full UUID compatibility
- URL-safe characters only
The base62 encoding/decoding operations are optimized for performance. The package uses minimal memory allocations and efficient algorithms for conversions.
- Base62 encoding of uint64 values must fit within 11 characters
- String representations are always 22 characters
- UUID conversions must be valid UUID format
All KUID operations are thread-safe. The package can be safely used in concurrent applications.
The package provides specific error types for different failure cases:
ErrInvalidLength
: Input string has incorrect lengthErrInvalidChar
: Invalid character in input stringErrInvalidUUID
: Malformed UUID string
Contributions are welcome! Please feel free to submit a Pull Request.