gouuidv6

package module
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2021 License: MIT Imports: 8 Imported by: 0

README

Go Report Card tests race static

Reference implementation of the draft "Version 6" UUID proposal in Go

Draft proposal

The draft proposal of the UUIDv6 spec along with examples and rationale is available here.

Another informative source which contains reference implementations for generating UUIDv6's in go and converting UUIv1 to UUIDv6 in Python is available here.

Byte layout

Bytes 0-7: (each digit shown is hex, 4 bits)

    00 00 00 00  00 00 00 00
    |                | ||  |
     ----------------  ||  |
    timestamp          ||  |
    bits 59-12         ||  |
                 version|  |
                         --
                  timestamp
                  bits 11-0

Bytes 8-15: (same as RFC 4122)

    00 00 00 00  00 00 00 00
    ||  | |                |
    ||  |  ________________
    ||  |       node
    | --
    | clock seq bits 11-0
    2 bits variant, 2 bits
    are 13-12 of clock seq

Usage

Usage is pretty simple, the only variable is the node id used. The most basic usage is to allow the library to use the MAC address as the node id.

id := gouuidv6.New()
id.String()

An alternative is to use a randomized node ID

gouuidv6.AlwaysRandomizeNode()
id := gouuidv6.New()
id.String()

Documentation

Overview

Package gouuidv6 implements "Version 6" UUIDs in Go. See http://bradleypeabody.github.io/uuidv6/ UUIDs sort correctly by time when naively sorted as raw bytes, have a Time() function that returns time the UUID was created and have a reasonable guarantee of being globally unique (based on the specifications from RFC 4122, with a few intentional exceptions.)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlwaysRandomizeNode

func AlwaysRandomizeNode()

AlwaysRandomizeNode sets the uuid generation in such way that each uuid has a random

func GetNode

func GetNode() uint64

GetNode returns the node id this instance is using

func RandomizeNode

func RandomizeNode()

RandomizeNode sets the 'node' part of the UUID to a random value, instead of using one of the MAC addresses from the system. Use this if you are concerned about the privacy aspect of using a MAC address.

func SetNode added in v0.1.19

func SetNode(nodeID uint64)

SetNode sets the node used for uuidv6's

Types

type UUID

type UUID [16]byte

UUID represents a "Version 6" UUID.

func New

func New() UUID

New returns a new UUID initialized to a proper value according to "Version 6" rules.

func NewFromTime

func NewFromTime(t time.Time) UUID

NewFromTime returns a new UUID set to the given time

func Parse

func Parse(us string) (UUID, error)

Parse text representation into a UUID struct

func ParseBytes

func ParseBytes(bs []byte) (UUID, error)

ParseBytes parses a slice of bytes into a UUID struct

func (UUID) Bytes

func (u UUID) Bytes() []byte

Bytes returns UUID as byte slice

func (UUID) Compare

func (u UUID) Compare(to UUID) bool

Compare two UUIDs and return true of their lower 8 bytes

func (UUID) HighBytes added in v0.1.19

func (u UUID) HighBytes() []byte

HighBytes returns the first 8 bytes of a UUID

func (UUID) IsNil

func (u UUID) IsNil() bool

IsNil returns true if all UUID bytes are zero

func (UUID) LowBytes added in v0.1.19

func (u UUID) LowBytes() []byte

LowBytes returns the last 8 bytes of a UUID

func (UUID) MarshalBinary

func (u UUID) MarshalBinary() ([]byte, error)

MarshalBinary returns a UUID as a slice of bytes

func (UUID) MarshalJSON

func (u UUID) MarshalJSON() ([]byte, error)

MarshalJSON allows the UUID struct to be seamlessly used as a native json type

func (UUID) MarshalText

func (u UUID) MarshalText() ([]byte, error)

MarshalText returns the String representation of a UUID as a slice of bytes

func (UUID) Node

func (u UUID) Node() uint64

Node extracts and return the node from the UUID

func (*UUID) Scan

func (u *UUID) Scan(value interface{}) error

Scan allows the UUID struct to be seamlessly used as a native SQL type

func (UUID) String

func (u UUID) String() string

String returns a textual representation per RFC 4122, e.g. "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"

func (UUID) Time

func (u UUID) Time() time.Time

Time extracts and return the time from the UUID

func (*UUID) UnmarshalBinary

func (u *UUID) UnmarshalBinary(data []byte) error

UnmarshalBinary updates a UUID struct using a slice of bytes representing a UUID

func (*UUID) UnmarshalJSON

func (u *UUID) UnmarshalJSON(data []byte) error

UnmarshalJSON allows the UUID struct to be seamlessly used as a native json type

func (*UUID) UnmarshalText

func (u *UUID) UnmarshalText(text []byte) (err error)

UnmarshalText updates a UUID struct using a slice of bytes representing a UUID in string format

func (UUID) Value

func (u UUID) Value() (driver.Value, error)

Value allows the UUID struct to be seamlessly used as a native SQL type

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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