CLID for Go
A fast, thread-safe Go library for generating CLIDs (Chrono-Lexical Identifiers). These K-Sortable, 64-bit IDs are perfect for use as database primary keys.
This package provides a simple and efficient implementation of the CLID specification, making it easy to generate unique, time-sortable identifiers in your Go applications.
Features
- High Performance: Generates millions of IDs per second with low overhead.
- Thread-Safe: Safe for concurrent use in goroutines.
- Database Friendly: Implements sql.Scanner and driver.Valuer for easy database integration.
- Dual Representation: Use BIGINT for maximum performance and VARCHAR for readability.
Structure
- Unix Timestamp, 42 bits
- Machine ID, 8 bits
- Randomly generated number, 14 bits
Installation
To get started, use go get to add the package to your project.
go install github.com/igarciacracco/clid@latest
Usage
Here is a simple example of how to generate a new CLID.
package main
import (
"fmt"
"log"
"github.com/igarciacracco/clid"
)
func main() {
// Generate a new CLID
id, err := clid.New()
if err != nil {
log.Fatalf("Failed to generate CLID: %v", err)
}
// Print its different representations
fmt.Printf("String representation: %s\n", id.String()) // e.g., "2025-12-30T08:11:40.212-G-10213"
fmt.Printf("Integer representation: %d\n", id.Int64()) // e.g., 29646902634371033061
fmt.Printf("Timestamp: %v\n", id.Timestamp) // e.g., 2023-08-29 10:00:00 +0000 UTC
}
License
This project is licensed under the MIT License. See the LICENSE file for details.