Documentation ¶
Overview ¶
Package iid generates small, globally unique IDs.
Index ¶
Examples ¶
Constants ¶
const ( // IidLen is the length of the byte slice used for the Iid type. IidLen = 8 // StrLen is the length of iid string representations in bytes. StrLen = 11 )
Variables ¶
var Enc = base64.NewEncoding(encoder)
The encoding used for string serialization and deserialization of iids.
var ErrInvalid = errors.New("invalid id")
ErrInvalid is returned when trying to import an invalid Iid from a string.
var RandReader = rand.Reader
RandReader is used to fill the 31 least significant bits of new iids with random values.
var Timestamp = func() uint32 { return uint32(time.Now().Unix()) }
Timestamp is used to get the current UNIX time in seconds when generating new iids.
Functions ¶
func IsErrInvalid ¶
IsErrInvalid returns true if the cause of the provided error is ErrInvalid.
Types ¶
type Iid ¶
type Iid []byte
Iid represents time sortable ID which can be exported as a base64url encoded string, int64 or uint64.
func FromInt ¶ added in v1.0.2
FromInt imports an existing Iid from its int64 representation.
Example ¶
var i int64 = 6711382541547442289 id := FromInt(i) fmt.Println(id.Int())
Output: 6711382541547442289
func FromString ¶
FromString imports an existing Iid from its base64url encoded string representation.
Example ¶
str := "MHDSedbNhXB" s, err := FromString(str) if err != nil { log.Fatal(err) } fmt.Println(s.String())
Output: MHDSedbNhXB
func FromUint64 ¶
FromUint64 imports an existing Iid from its uint64 representation.
Example ¶
var i uint64 = 6711382541547442289 id := FromUint64(i) fmt.Println(id.Uint64())
Output: 6711382541547442289
func New ¶
func New() Iid
New generates a completely new Iid containing: 1 bit empty, 32 bit current timestamp, 31 bit cryptographically secure random bits.
Example ¶
id := New() fmt.Println(id)
Output: Ad7YmV-----