objectid

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2023 License: Apache-2.0, BSD-3-Clause, MIT Imports: 6 Imported by: 2

README

objectid

GoDoc Build Status Coverage Status Stable

Mongo object ID with 16-byte-length support (32 bytes in string), which could be replacement of UUID.

What Is It for

MongoDB Object ID is a practical universal uniq ID solutions. But in some case, we need a 32-bytes long string to act like UUID, which also provides create time within. This what this package for.

The simplest way to implement 16-bytes long object ID (32 bytes as hex string) is adding random binaries in the tailing. However, I think nanoseconds are also quite randomized enough, thus I use nanoseconds instead.

How to Use

Create Object ID

To generate a 12-bytes standard object ID, Just use New12:

id12 := objectid.New12()

It will return a objectid.ObjectID, equavilent to []byte.

or you may use specified time:

t := time.Now()
id12 := objectid.New12(t)

To generate a 16-bytes extended object ID, Just use New16, with the same usage of New12.

Access Create Time

With objectid.ObjectID type, use Time() to get create time:

id12 := objectid.New12()
id16 := objectid.New16()
tm12 := id12.Time()     // return time with accuracy of seconds
tm16 := id16.Time()     // return time with accuracy of nanoseconds, or microseconds in some OS
Convert to Hexadecimal String

Use function String() to achieve this.

Other Informations

License codebeat badge

Documentation

Overview

Package objectid provides MongoDB-objectId-like with 16-byte-length support (32 bytes in string). This could be replacement of UUID.

The simplest way to implement 16-bytes long object ID (32 bytes as hex string) is adding random binaries in the tailing. However, as nanoseconds are also quite randomized enough, thus I use nanoseconds instead.

This objectId has many advantages comparing to UUID:
1. An ObjectID has timestamp information, which can be very useful in many case. Such as database sharding.
2. An ObjectID is lead by timestamp, therefore it can be ordered.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ObjectID

type ObjectID []byte

ObjectID is a series of bytes with length of 12 or 16. The 16-bytes-lengthed ObjectID is compatable with standard MongoDB objectId.

func New12

func New12(t ...time.Time) ObjectID

New12 generates a standard MongoDB object ID. The time.Time parameter is optional. If no time given, it generates ObjectID with current time.

func New16

func New16(t ...time.Time) ObjectID

New16 generates a extended MongoDB object ID, with tailing 4 bytes of nanoseconds. The time.Time parameter is optional. If no time given, it generates ObjectID with current time.

func NewByBytes added in v1.0.2

func NewByBytes(b []byte) (ObjectID, error)

NewByBytes parse a objectid from given byte slice

func NewByHex added in v1.0.1

func NewByHex(s string) (ObjectID, error)

NewByHex parse a objectid from given hex string

Example
package main

import (
	"fmt"

	objectid "github.com/Andrew-M-C/go.objectid"
)

func main() {
	hex := "5DA5360F51AD44C91EB2C7291D946728"
	o, err := objectid.NewByHex(hex)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Println("hex:", o)
}
Output:

hex: 5da5360f51ad44c91eb2c7291d946728

func (ObjectID) Len

func (id ObjectID) Len() int

Len is equivalent with len(id)

func (ObjectID) String

func (id ObjectID) String() string

String returns object id in hex format.

func (ObjectID) Time

func (id ObjectID) Time() time.Time

Time returns the time information stored in object ID. If the id is extended (16 bytes length), nanoseconds will also be parsed.

Example
package main

import (
	"fmt"
	"time"

	objectid "github.com/Andrew-M-C/go.objectid"
)

func main() {
	t := time.Date(2020, 1, 1, 12, 0, 0, 123456789, time.UTC)
	id := objectid.New16(t)
	fmt.Println("time: ", id.Time())
}
Output:

time:  2020-01-01 12:00:00.123456789 +0000 UTC

func (ObjectID) UnixMilli added in v1.0.3

func (id ObjectID) UnixMilli() int64

UnixMilli is equivalent id.Time().UnixNano / time.Millisecond

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL