uuid

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2018 License: MIT Imports: 14 Imported by: 1

README

uuid (Universally Unique IDentifier generator for Go)

Build status Build status Sourcegraph GoDoc Minimal version

About

This package is a UUID (or GUID) generator for Go.

Supported versions:
Version Supported
1
2
3
4
5

Usage

Full documentation here.

Installing
Go 1.10

vgo get -u github.com/gbrlsnchs/uuid

Go 1.11 or after

go get -u github.com/gbrlsnchs/uuid

Importing
import (
	// ...

	"github.com/gbrlsnchs/uuid"
)

Example

Generating UUIDs
guid := uuid.V4(nil)                            // panics if there's an error
log.Printf("guid = %v", guid)                   // prints a 36-byte hex-encoded UUID
log.Printf("guid version = %v", guid.Version()) // prints "Version 4"
log.Printf("guid variant = %v", guid.Variant()) // prints "RFC 4122"
Building UUIDs from strings
guid, err := uuid.Parse("d9ab3f01-482f-425d-8a10-a24b0abfe661")
if err != nil {
	// handle error
}
log.Print(guid.String())           // prints "d9ab3f01-482f-425d-8a10-a24b0abfe661"
log.Print(guid.GUID())             // prints "{d9ab3f01-482f-425d-8a10-a24b0abfe661}"
log.Print(guid.Version().String()) // prints "Version 4"
log.Print(guid.Variant().String()) // prints "RFC 4122"

Contributing

How to help

Documentation

Overview

Package uuid is a universally unique identifier generator.

Index

Constants

This section is empty.

Variables

View Source
var (
	// NamespaceDNS is the namespace UUID defined for DNS.
	NamespaceDNS, _ = Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
	// NamespaceURL is the namespace UUID defined for URL.
	NamespaceURL, _ = Parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
	// NamespaceOID is the namespace UUID defined for ISO OID.
	NamespaceOID, _ = Parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8")
	// NamespaceX500 is the namespace UUID defined for X.500 DN.
	NamespaceX500, _ = Parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
)
View Source
var (
	// Null is a null UUID that translates to a 36-byte string with only zeroes.
	Null = UUID{}
	// ErrInvalid is the error for invalid UUID formats.
	ErrInvalid = errors.New("uuid: invalid uuid")
)

Functions

This section is empty.

Types

type UUID

type UUID [byteSize]byte

UUID is a 16-byte array Universally Unique IDentifier, as per the RFC 4122.

func GenerateV1 added in v0.3.0

func GenerateV1(random bool) (UUID, error)

GenerateV1 generates a version 1 UUID.

func GenerateV2 added in v0.3.0

func GenerateV2(id uint32, ldn uint8, random bool) (UUID, error)

GenerateV2 generates a version 2 UUID.

It basically returns a version 1 UUID but overrides the least significant 8 bits of the clock sequence by the variable "ldn", a local domain name, and the least significant 32 bits of the timestamp an integer identifier "id".

func GenerateV3 added in v0.3.0

func GenerateV3(ns UUID, data []byte) (UUID, error)

GenerateV3 generates a version 3 UUID based on a namespace UUID and additional data.

func GenerateV4 added in v0.3.0

func GenerateV4(r io.Reader) (UUID, error)

GenerateV4 generates a version 4 UUID.

If a nil reader is passed as argument, crypto/rand.Reader is used instead.

func GenerateV5 added in v0.3.0

func GenerateV5(ns UUID, data []byte) (UUID, error)

GenerateV5 generates a version 5 UUID based on a namespace UUID and additional data.

func Parse

func Parse(s string) (UUID, error)

Parse parses a UUID 36-byte slice encoded in hexadecimal and converts it to a 16-byte array.

func V1 added in v0.6.0

func V1(random bool) UUID

V1 returns a version 1 UUID or panics otherwise.

func V2 added in v0.6.0

func V2(id uint32, ldn uint8, random bool) UUID

V2 returns a version 2 UUID or panics otherwise.

func V3 added in v0.6.0

func V3(ns UUID, data []byte) UUID

V3 returns a version 3 UUID or panics otherwise.

func V4 added in v0.6.0

func V4(r io.Reader) UUID

V4 returns a version 4 UUID or panics otherwise.

If a nil reader is passed as argument, crypto/rand.Reader is used instead.

func V5 added in v0.6.0

func V5(ns UUID, data []byte) UUID

V5 returns a version 5 UUID or panics otherwise.

func (UUID) GUID

func (guid UUID) GUID() string

GUID returns a 36-byte string with surrounding curly braces.

func (UUID) IsZero added in v0.6.0

func (guid UUID) IsZero() bool

IsZero returns whether the UUID is a null UUID.

func (UUID) MarshalBinary

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

MarshalBinary implements binary marshaling.

func (UUID) MarshalJSON

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

MarshalJSON implements JSON marshaling.

func (UUID) MarshalText

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

MarshalText implements text marshaling.

func (*UUID) Scan added in v0.6.0

func (guid *UUID) Scan(v interface{}) error

Scan implements scanning a UUID from an SQL database.

func (UUID) String

func (guid UUID) String() string

String converts the 16-byte UUID to a 36-byte string encoded in hexadecimal.

func (UUID) URN

func (guid UUID) URN() string

URN returns the UUID as string conformed to RFC 2141.

func (*UUID) UnmarshalBinary

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

UnmarshalBinary implements binary unmarshaling.

func (*UUID) UnmarshalJSON

func (guid *UUID) UnmarshalJSON(b []byte) error

UnmarshalJSON implements JSON unmarshaling.

func (*UUID) UnmarshalText

func (guid *UUID) UnmarshalText(b []byte) error

UnmarshalText implements text unmarshaling.

func (UUID) Value added in v0.6.0

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

Value implements saving the UUID as a 36-byte string encoded to hex to an SQL database.

func (UUID) Variant

func (guid UUID) Variant() Variant

Variant parses the variant from the UUID.

func (UUID) Version

func (guid UUID) Version() Version

Version extracts the version from the UUID.

type Variant

type Variant byte

Variant is a UUID variant as per the RFC 4122, § 4.1.1.

const (
	// VariantNCS is a reserved variant for NCS backward compatibility.
	VariantNCS Variant = 0x00
	// VariantRFC4122 is the current variant defined by the RFC 4122.
	VariantRFC4122 Variant = 0x80 // 10xxxxxx
	// VariantMicrosoft is a reserved variant for Microsoft Corporation backward compatibility.
	VariantMicrosoft Variant = 0xC0 // 110xxxxx
	// VariantUndefined is a reserved variant for future definition (still undefined).
	VariantUndefined Variant = 0xE0 // 111xxxxx

)

func (Variant) String

func (v Variant) String() string

type Version

type Version byte

Version is a modifying byte.

const (
	// VersionNone is an invalid UUID version.
	VersionNone Version = iota
	// Version1 is the version 1 of the UUID implementation.
	Version1 Version = iota << 4
	// Version2 is the version 2 of the UUID implementation.
	Version2
	// Version3 is the version 3 of the UUID implementation.
	Version3
	// Version4 is the version 4 of the UUID implementation.
	Version4
	// Version5 is the version 5 of the UUID implementation.
	Version5
)

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