Documentation
¶
Overview ¶
Package kid (K-sortable ID) provides a goroutine-safe generator of short (10 byte binary, 16 bytes when base32 encoded), url-safe, k-sortable unique IDs.
The 10-byte binary representation of an ID is composed of:
- 6-byte value representing Unix time in milliseconds
- 2-byte sequence, and,
- 2-byte random value.
IDs encode (base32) as 16-byte url-friendly strings.
kid.ID features:
- Size: 10 bytes as binary, 16 bytes if stored/transported as an encoded string.
- Timestamp + sequence is guaranteed to be unique.
- 2 bytes of trailing randomness
- K-orderable in both binary and base32 encoded representations.
- URL-friendly custom encoding without the vowels a, i, o, and u.
- Automatic (un)/marshalling for SQL and JSON.
- The cmd/kid tool for ID generation and introspection.
Example usage:
func main() { id := kid.New() fmt.Printf("%s %s %03v\n", id, id.String(), id[:]) // Example output: 06bq7xhnr03mlz6r 06bq7xhnr03mlz6r [001 149 115 246 021 192 007 073 252 216] id, err := kid.FromString("06bq7xhnr03mlz6r") if err != nil { // do something } fmt.Printf("%s %s %03v\n", id, id.String(), id[:]) // Output: 06bq7xhnr03mlz6r 06bq7xhnr03mlz6r [001 149 115 246 021 192 007 073 252 216] }
Acknowledgments:
While the ID payload differs greatly, the API and much of this package borrows heavily from https://github.com/rs/xid, a zero-configuration globally-unique ID generator. ID unique timestamp+sequence pairs are generated from the google/uuidV7 getV7Time() algorithm.
Index ¶
- Variables
- func Sort(ids []ID)
- type ID
- func (id ID) Bytes() []byte
- func (id ID) Compare(other ID) int
- func (id ID) Encode(dst []byte) []byte
- func (id ID) IsNil() bool
- func (id ID) IsZero() bool
- func (id ID) MarshalJSON() ([]byte, error)
- func (id ID) MarshalText() ([]byte, error)
- func (id ID) Random() int32
- func (id *ID) Scan(value any) error
- func (id ID) Sequence() int32
- func (id ID) String() string
- func (id ID) Time() time.Time
- func (id ID) Timestamp() int64
- func (id *ID) UnmarshalJSON(b []byte) error
- func (id *ID) UnmarshalText(text []byte) error
- func (id ID) Value() (driver.Value, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidID represents an error state, typically when decoding invalid input ErrInvalidID = errors.New("kid: invalid id") )
Functions ¶
Types ¶
type ID ¶
type ID [rawLen]byte
ID represents a unique identifier
func FromString ¶
FromString decodes a base32-encoded string to return an ID.
Example ¶
id, err := FromString("03f6nlxczw0018fz") if err != nil { panic(err) } fmt.Println(id.Timestamp(), id.Random())
Output: 946684799999 41439
func New ¶
func New() (id ID)
New generates a new unique ID.
This function is goroutine-safe. IDs are composed of:
- 6 bytes, timestamp, a Unix time in milliseconds
- 2 bytes, sequence, a derived value ensuring uniqueness and order
- 2 bytes, random value provided by crypto/rand
K-orderable: Each subsequent call to New() is guaranteed to produce an ID having a timestamp + sequence value greater than the previously generated ID.
Example ¶
examples
id := New() fmt.Printf(`ID: String() %s Timestamp() %d Sequence() %d Random() %d Time() %v Bytes() %3v\n`, id.String(), id.Timestamp(), id.Sequence(), id.Random(), id.Time().UTC(), id.Bytes())
Output:
func (ID) Compare ¶
Compare makes IDs k-sortable, behaving like `bytes.Compare`, returning 0 if two IDs are identical, -1 if the current ID is less than the other, and 1 if current ID is greater than other.
Note: only the first 8 bytes of the two IDs (timestamp+sequence) are compared.
func (ID) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
A json value will always be returned; as a nilID or any other binary ID will always encode, error will always be nil.
func (ID) MarshalText ¶
MarshalText implements `encoding.TextMarshaler`.
As any ID value will always encode, error is always nil. https://golang.org/pkg/encoding/#TextMarshaler
func (*ID) Scan ¶
Scan implements the sql.Scanner interface. https://pkg.go.dev/database/sql#Scanner
func (ID) String ¶
String implements `fmt.Stringer`, returning id as a base32 encoded string using the kid custom character set. https://pkg.go.dev/fmt#Stringer
func (ID) Time ¶
Time returns the ID's timestamp as a Time value with millisecond resolution and location set to UTC
func (ID) Timestamp ¶
Timestamp returns the timestamp component of id as milliseconds since the Unix epoch. Go timestamps are at location UTC.
func (*ID) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface. https://golang.org/pkg/encoding/json/#Unmarshaler
func (*ID) UnmarshalText ¶
UnmarshalText implements `encoding.TextUnmarshaler`, and performs a sanity check on text.
Note: decode() is only called from here and should never fail. https://pkg.go.dev/encoding#TextUnmarshaler
Directories
¶
Path | Synopsis |
---|---|
cmd
|
|
kid
A utility to generate or inspect kid.IDs.
|
A utility to generate or inspect kid.IDs. |
eval
|
|
compare
Package main produces for comparison purposes a markdown formatted table illustrating key differences between a number of unique ID packages.
|
Package main produces for comparison purposes a markdown formatted table illustrating key differences between a number of unique ID packages. |
uniqcheck
Package main provides a test to determine if ID generation delivers unique IDs for Go applications utilizing concurrency.
|
Package main provides a test to determine if ID generation delivers unique IDs for Go applications utilizing concurrency. |