clid

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2025 License: MIT Imports: 6 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure added in v0.0.1

func Configure(NodeID string, Mode CLIDMode)

Types

type CLID

type CLID struct {
	Timestamp time.Time
	NodeID    string
	Code      uint16

	CodeBytes uint64
	// contains filtered or unexported fields
}

func New added in v0.0.1

func New() CLID

New Creates a new instance of a CLID object

func ParseLong

func ParseLong(int64) (CLID, error)

func ParseString

func ParseString(s string) (CLID, error)

func (CLID) Long

func (c CLID) Long() uint64

func (*CLID) Scan

func (c *CLID) Scan(src interface{}) error

Scan This allows you to do `db.QueryRow(...).Scan(&user.ID)` directly.

func (CLID) String

func (c CLID) String() string

func (CLID) Value

func (c CLID) Value() (driver.Value, error)

Value This allows you to use a CLID object as a parameter in a SQL query.

type CLIDMode added in v0.0.1

type CLIDMode int8
const (
	ModePerformance CLIDMode = iota
	ModeReadable
)

type Config added in v0.0.1

type Config struct {
	NodeID string
	Mode   CLIDMode
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL