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 ¶
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 ¶
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
NewByBytes parse a objectid from given byte slice
func NewByHex ¶ added in v1.0.1
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) 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