uuid package - github.com/satori/go.uuid - Go Packages (original) (raw)

Package uuid provides implementation of Universally Unique Identifier (UUID). Supported versions are 1, 3, 4 and 5 (as specified in RFC 4122) and version 2 (as specified in DCE 1.1).

UUID versions

UUID layout variants.

UUID DCE domains.

Size of a UUID in bytes.

View Source

var ( NamespaceDNS = Must(FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")) NamespaceURL = Must(FromString("6ba7b811-9dad-11d1-80b4-00c04fd430c8")) NamespaceOID = Must(FromString("6ba7b812-9dad-11d1-80b4-00c04fd430c8")) NamespaceX500 = Must(FromString("6ba7b814-9dad-11d1-80b4-00c04fd430c8")) )

Predefined namespace UUIDs.

Nil is special form of UUID that is specified to have all 128 bits set to zero.

func Equal(u1 UUID, u2 UUID) bool

Equal returns true if u1 and u2 equals, otherwise returns false.

type Generator interface { NewV1() UUID NewV2(domain byte) UUID NewV3(ns UUID, name string) UUID NewV4() UUID NewV5(ns UUID, name string) UUID }

Generator provides interface for generating UUIDs.

type NullUUID struct { UUID UUID Valid bool }

NullUUID can be used with the standard sql package to represent a UUID value that can be NULL in the database

func (u *NullUUID) Scan(src interface{}) error

Scan implements the sql.Scanner interface.

Value implements the driver.Valuer interface.

UUID representation compliant with specification described in RFC 4122.

FromBytes returns UUID converted from raw byte slice input. It will return error if the slice isn't 16 bytes long.

func FromBytesOrNil(input []byte) UUID

FromBytesOrNil returns UUID converted from raw byte slice input. Same behavior as FromBytes, but returns a Nil UUID on error.

FromString returns UUID parsed from string input. Input is expected in a form accepted by UnmarshalText.

func FromStringOrNil(input string) UUID

FromStringOrNil returns UUID parsed from string input. Same behavior as FromString, but returns a Nil UUID on error.

Must is a helper that wraps a call to a function returning (UUID, error) and panics if the error is non-nil. It is intended for use in variable initializations such as

var packageUUID = uuid.Must(uuid.FromString("123e4567-e89b-12d3-a456-426655440000"));

NewV1 returns UUID based on current timestamp and MAC address.

func NewV2(domain byte) UUID

NewV2 returns DCE Security UUID based on POSIX UID/GID.

NewV3 returns UUID based on MD5 hash of namespace UUID and name.

NewV4 returns random generated UUID.

NewV5 returns UUID based on SHA-1 hash of namespace UUID and name.

func (u UUID) Bytes() []byte

Bytes returns bytes slice representation of UUID.

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

MarshalBinary implements the encoding.BinaryMarshaler interface.

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

MarshalText implements the encoding.TextMarshaler interface. The encoding is the same as returned by String.

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

Scan implements the sql.Scanner interface. A 16-byte slice is handled by UnmarshalBinary, while a longer byte slice or a string is handled by UnmarshalText.

func (u *UUID) SetVariant(v byte)

SetVariant sets variant bits.

func (u *UUID) SetVersion(v byte)

SetVersion sets version bits.

Returns canonical string representation of UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.

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

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. It will return error if the slice isn't 16 bytes long.

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

UnmarshalText implements the encoding.TextUnmarshaler interface. Following formats are supported:

"6ba7b810-9dad-11d1-80b4-00c04fd430c8", "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}", "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8" "6ba7b8109dad11d180b400c04fd430c8"

ABNF for supported UUID text representation follows:

uuid := canonical | hashlike | braced | urn plain := canonical | hashlike canonical := 4hexoct '-' 2hexoct '-' 2hexoct '-' 6hexoct hashlike := 12hexoct braced := '{' plain '}' urn := URN ':' UUID-NID ':' plain URN := 'urn' UUID-NID := 'uuid' 12hexoct := 6hexoct 6hexoct 6hexoct := 4hexoct 2hexoct 4hexoct := 2hexoct 2hexoct 2hexoct := hexoct hexoct hexoct := hexdig hexdig hexdig := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F'

Value implements the driver.Valuer interface.

func (u UUID) Variant() byte

Variant returns UUID layout variant.

func (u UUID) Version() byte

Version returns algorithm version used to generate UUID.