Skip to content

Latest commit

 

History

History
61 lines (43 loc) · 1.63 KB

File metadata and controls

61 lines (43 loc) · 1.63 KB

Globally Unique ID Generator

godoc license Build Status Coverage

Package goid is a globally unique id generator library, ready to be used safely directly in your server code.

goid is using 128bit and hashids algorithm to generate globally unique ids to make it shorter when transported as a string: https://github.com/speps/go-hashids

  • 8-byte value representing the milliseconds since the Unix epoch,
  • 4-byte counter, starting with a random value,
  • 6-bit server room id,
  • 8-bit cluster id,
  • 9-bit machine id,
  • 3-bit work id, and
  • 6-bit op id.

The string representation is using hashids to generates (28bytes).

Features:

  • Size: 16 bytes (128 bits)
  • Lock-free (i.e.: unlike UUIDv1 and v2)

Install

go get -u github.com/speps/go-hashids
go get -u github.com/jefurry/goid

Usage

import (
    "github.com/jefurry/goid"
)

id := goid.New()

s, _ := id.Encode(0, 0, 0, 0, 0)
err := id.FromString(s)

func init() {
    goid.SeedMathRand()
}

Get goid embedded info:

id.Timestamp()
id.Counter()
id.ServerRoomID()
id.ClusterID()
id.MachineID()
id.WorkID()
id.OpID()

Licenses

All source code is licensed under the BSD License.