uuid

package module
v0.0.0-...-d06bb7f Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2019 License: MIT Imports: 5 Imported by: 3

README

GoDoc Build Status Go Report Card

uuid

This package generates a 128-bit UUID based on Unix time (similar to one of CouchDB's UUID options).

The first 7 bytes indicate the time since the unix epoch in hex microseconds; the last 9 bytes are cryptographically secure random bits.

The benefits of this form of UUID are chronological sortability, which is good for database key usage, and a measure of protection against predictability, since it carries at least 72 bits of entropy (9 x 8b). Note that this doesn't necessarily make it a good choice for cryptographic purposes.

This package defaults to the UUID form without dashes, as they're not particularly useful in this format.

Licensed under MIT/ISC license. Use freely. Fixes and improvements welcome.

Documentation

Overview

Package uuid generates a 128-bit UUID based on Unix time (similar to one of CouchDB's UUID options).

For example:

0505 da61 3800 49RR RRRR RRRR RRRR RRRR
----------------- 7 bytes, unix time in hex microseconds
                 ---------------------- 9 bytes, random data

The benefits of this form of UUID are chronological sortability, which is good for database key usage, and a measure of protection against predictability, since it carries at least 72 bits of entropy (9 x 8b). Note that this doesn't necessarily make it a good choice for cryptographic purposes.

This package defaults to the UUID form without dashes, as they're not particularly useful in this format.

Index

Constants

View Source
const (
	UUID_BYTES                     = 16
	UUID_RANDOM_BYTES              = 9
	UUID_RANDOM_OFFSET             = 7
	UUID_STRING_LENGTH             = 32
	UUID_STRING_LENGTH_WITH_DASHES = 36

	ByteSize               = 16
	StringLength           = 32
	StringLengthWithDashes = 36
)

document offsets and lengths

Variables

View Source
var (
	// ErrInvalidUUID means we failed to parse the given uuid.
	ErrInvalidUUID = errors.New("error parsing uuid")
)

Functions

func Equal

func Equal(u1 UUID, u2 UUID) bool

Equal returns true if two UUIDs are equal.

Types

type UUID

type UUID [UUID_BYTES]byte

UUID is an alias for an array of 16 bytes.

func FromBytes

func FromBytes(b []byte) (UUID, error)

FromBytes takes a byte slice and returns a UUID and optionally an error.

func FromString

func FromString(s string) (UUID, error)

FromString returns a UUID object from a given string and optionally an error.

func FromStringUnsafe

func FromStringUnsafe(s string) UUID

FromStringUnsafe returns a UUID object from a given string, ignoring any errors. (This function just calls FromString() and throws away the error.)

func MustFromString

func MustFromString(s string) UUID

MustFromString returns a UUID object from a given string. It panics if the string can't be parsed. (This function just calls FromString() and panics on error.)

func MustNewUUID

func MustNewUUID() UUID

MustNewUUID calls NewUUID and panics on error.

func NewUUID

func NewUUID() (UUID, error)

NewUUID creates a new unix time stamp based UUID.

func Nil

func Nil() UUID

Nil returns a UUID with all bytes set to zero.

func (*UUID) Array

func (u *UUID) Array() *[UUID_BYTES]byte

Array returns a ref to underlying type [16]byte, for modification.

func (UUID) Bytes

func (u UUID) Bytes() []byte

Bytes returns the UUID as a byte slice.

func (*UUID) IsNil

func (u *UUID) IsNil() bool

IsNil returns true if a UUID is unset.

func (UUID) MarshalBinary

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

MarshalBinary implements the encoding.BinaryMarshaler interface (since go 1.2).

func (UUID) MarshalText

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

MarshalText implements the encoding.TextMarshaler interface (since go 1.2).

func (UUID) String

func (u UUID) String() string

String returns the UUID in string form (without dashes).

func (UUID) ToTime

func (u UUID) ToTime() time.Time

ToTime converts the unix time stamp inside the UUID to a time.Time.

func (*UUID) UnmarshalBinary

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

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface (since go 1.2).

func (*UUID) UnmarshalText

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

UnmarshalText implements the encoding.TextUnmarshaler interface (since go 1.2).

Directories

Path Synopsis
Package uuidflag wraps a UUID so it satisfies the Value and Getter interface of the Go flag package.
Package uuidflag wraps a UUID so it satisfies the Value and Getter interface of the Go flag package.

Jump to

Keyboard shortcuts

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