uuid

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: BSD-3-Clause, BSD-3-Clause Imports: 7 Imported by: 0

README

Provenance: This package was adapted from https://github.com/google/uuid/tree/v1.1.0 to minimize external dependencies of generated code. We use a subset of the API and have removed all references to non-V4 UUIDs.


uuid build status

The uuid package generates and inspects UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.

This package is based on the github.com/pborman/uuid package (previously named code.google.com/p/go-uuid). It differs from these earlier packages in that a UUID is a 16 byte array rather than a byte slice. One loss due to this change is the ability to represent an invalid UUID (vs a NIL UUID).

Install

go get github.com/google/uuid

Documentation

GoDoc

Full go doc style documentation for the package can be viewed online without installing this package by using the GoDoc site here: http://godoc.org/github.com/google/uuid

Documentation

Overview

Package uuid generates and inspects UUIDs.

UUIDs are based on RFC 4122 and DCE 1.1: Authentication and Security Services.

A UUID is a 16 byte (128 bit) array. UUIDs may be used as keys to maps or compared directly.

Index

Constants

View Source
const (
	Invalid   = Variant(iota) // Invalid UUID
	RFC4122                   // The variant specified in RFC4122
	Reserved                  // Reserved, NCS backward compatibility.
	Microsoft                 // Reserved, Microsoft Corporation backward compatibility.
	Future                    // Reserved for future definition.
)

Constants returned by Variant.

Variables

This section is empty.

Functions

func SetRand

func SetRand(r io.Reader)

SetRand sets the random number generator to r, which implements io.Reader. If r.Read returns an error when the package requests random data then a panic will be issued.

Calling SetRand with nil sets the random number generator to the default generator.

Types

type UUID

type UUID [16]byte

A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC 4122.

func FromBytes

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

FromBytes creates a new UUID from a byte slice. Returns an error if the slice does not have a length of 16. The bytes are copied from the slice.

func Must

func Must(uuid UUID, err error) UUID

Must returns uuid if err is nil and panics otherwise.

func MustParse

func MustParse(s string) UUID

MustParse is like Parse but panics if the string cannot be parsed. It simplifies safe initialization of global variables holding compiled UUIDs.

func New

func New() UUID

New creates a new random UUID or panics. New is equivalent to the expression

uuid.Must(uuid.NewRandom())

func NewRandom

func NewRandom() (UUID, error)

NewRandom returns a Random (Version 4) UUID.

The strength of the UUIDs is based on the strength of the crypto/rand package.

A note about uniqueness derived from the UUID Wikipedia entry:

Randomly generated UUIDs have 122 random bits.  One's annual risk of being
hit by a meteorite is estimated to be one chance in 17 billion, that
means the probability is about 0.00000000006 (6 × 10−11),
equivalent to the odds of creating a few tens of trillions of UUIDs in a
year and having one duplicate.

func Parse

func Parse(s string) (UUID, error)

Parse decodes s into a UUID or returns an error. Both the standard UUID forms of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded as well as the Microsoft encoding {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} and the raw hex encoding: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.

func ParseBytes

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

ParseBytes is like Parse, except it parses a byte slice instead of a string.

func (UUID) MarshalBinary

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

MarshalBinary implements encoding.BinaryMarshaler.

func (UUID) MarshalText

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

MarshalText implements encoding.TextMarshaler.

func (UUID) String

func (uuid UUID) String() string

String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx , or "" if uuid is invalid.

func (UUID) URN

func (uuid UUID) URN() string

URN returns the RFC 2141 URN form of uuid, urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid.

func (*UUID) UnmarshalBinary

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

UnmarshalBinary implements encoding.BinaryUnmarshaler.

func (*UUID) UnmarshalText

func (uuid *UUID) UnmarshalText(data []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

func (UUID) Variant

func (uuid UUID) Variant() Variant

Variant returns the variant encoded in uuid.

func (UUID) Version

func (uuid UUID) Version() Version

Version returns the version of uuid.

type Variant

type Variant byte

A Variant represents a UUID's variant.

func (Variant) String

func (v Variant) String() string

type Version

type Version byte

A Version represents a UUID's version.

func (Version) String

func (v Version) String() string

Jump to

Keyboard shortcuts

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