slid is a simple, 12-byte (96-bit) lexicographically-sortable identifier encoder for Go that embeds a nanosecond-resolution timestamp with a 32-bit atomically-incremented counter value. In other words, it generates IDs. Which are small. And probably unique (usually 2.14e22 unique identifiers per millsecond). And maybe usable.
Lexicographically-ordered IDs can be quite useful for key/value stores like LMDB and BoltDB in situations when you'd like for values to be time-sorted. The identifiers generated by slid sacrifice human readability for performance and identifier size versus, for example, RFC 3339-encoded timestamps.
NOTE: Since a SLID is generated via a counter value, it's best used only in long-running processes, and only when there's no vulnerability in a user being able to potentially jump an identifier to the next value in the sequence. Otherwise, prefer some sort of UUID.
This package uses Go modules. If you've installed Go, import the package:
import "github.com/boatilus/slid"
You can then run go mod tidy
to grab the package.
From there, slide into generating some slids ⛷️
id := slid.New() // constructs a new SLID struct with the current time
encodedID := id.Encode() // returns a []byte
decodedID := slid.Decode(encodedID) // returns a SLID struct
BenchmarkNew-4 50000000 30.2 ns/op
BenchmarkEncode-4 30000000 34.7 ns/op
BenchmarkNewThenEncode-4 30000000 52.3 ns/op
BenchmarkDecode-4 100000000 10.9 ns/op
(Run on a 2.7 GHz Haswell Core i5)
go test -v
This project is dedicated to the public domain via Creative Commons CC0 - see LICENSE.md for details.
- Inspired by Alizain Feerasta's ULID.