Code Monkey home page Code Monkey logo

uulid's Introduction

UULID Universally Unique Lexicographically Sortable Identifier

Build Status Go Report Card go.dev reference Apache 2 licensed

This package provides an implementation of the ULID spec compatible with the GUID/UUID format. It is based on the amazing oklog/ulid package, with the following differences:

  • Small and simple API
  • Encodes to a standard UUID format and can be used as the UUID type in databases
  • More safer and strict as it doesn't allow the generation time to travel backwards
  • A faster monotonic RNG that don't allocate memory unnecessarily
  • Defaults to text instead of binary when using the sql/driver.Valuer interface

Install

This package requires Go modules.

go get github.com/brunotm/uulid

Usage

    id, err := uulid.New()
    if err != nil {
        // handle err
    }
    fmt.Println(id.String()) // Output: 0178a727-335d-db2d-4671-c5c757718d7c

Test

go test ./...

Command line tool

This repo also provides a tool to generate and parse UULIDs at the command line.

go get -v github.com/brunotm/ulid/cmd/uulid

Usage:

Usage of uulid:
  -local
        when parsing, show local time instead of UTC
  -p string
        parse the given uulid

Specification

Timestamp

  • 48 bits
  • UNIX-time in milliseconds
  • Won't run out of space till the year 10889 AD

Entropy

Binary Layout and Byte Order

The components are encoded as 16 octets. Each component is encoded with the Most Significant Byte first (network byte order).

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                      32_bit_uint_time_high                    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|     16_bit_uint_time_low      |       16_bit_uint_random      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       32_bit_uint_random                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       32_bit_uint_random                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Encoding String Representation

This package encodes the ULID into the standard UUID format. (The ULID spec uses Crockford's Base32)

UULID: 0178a73c-3cc0-71ab-74b4-cc6c7190deae

 0178a73c3cc0      71ab74b4cc6c7190deae
|------------|    |--------------------|
   Timestamp             Entropy
    12 chars             20 chars
     48bits               80bits
     base32               base32

Prior Art

uulid's People

Contributors

brunotm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.