uuid

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2023 License: BSL-1.0 Imports: 9 Imported by: 0

README

UUID

Current release Documentation

This package implements the UUID versions defined in RFC4122bis.

Versions 1 through 5 are defined in the original RFC4122, while versions 6 through 8 are new additions.

Versions 6 and 7 are designed as timestamp-based, sortable IDs for use e.g. in database indexes. Version 7 is recommended for new applications while version 6 is a sortable replacement for version 1.

Version 8 is highly customizable, allowing for 122 custom bits with only version and variant being predefined.

Create a new UUID: uuid.NewVx() (UUID, error)

Convert an UUID to a string: UUID.String() string

Convert a string to an UUID: uuid.Parse(string) (UUID, error)

Convert a byte slice to an UUID: uuid.ParseBytes([]byte) (UUID, error)

Check if UUID contains only zeros: UUID.IsNil() bool

Check if UUID contains only ones: UUID.IsMax() bool

Support for encoding.Text(Un)Marshaler, encoding.Binary(Un)Marshaler, database/sql.Scanner and database/sql/driver.Valuer is built in, so the IDs can be used with most data exchange formats and databases.

Documentation

Index

Constants

View Source
const EpochToUnix int64 = 122192928000000000

EpochToUnix represents the 100ns intervals between 1582-10-15T00:00:00.00Z and 1970-01-01T00:00:00.00Z

Variables

View Source
var (
	UseHardwareMAC bool             = false // UseHardwareMAC defines whether to use a MAC address from a network card if available or generate one. It defaults to false for privacy reasons and because the MAC address lookup creates additional latency.
	RandomMAC      net.HardwareAddr         // Random MAC address - will be automatically generated unless set manually
)
View Source
var CurrentTime func() time.Time = time.Now

CurrentTime is a function used to get the current time. It defaults to time.Now but can be set to a different function in case a different time source should be used.

Functions

func MustString

func MustString(uuid UUID, err error) string

MustString works like Must but returns a string immediately

Types

type UUID

type UUID [16]byte

UUID represents a Universal Unique Identifier as an array containing 16 bytes

func Must

func Must(uuid UUID, err error) UUID

Must wraps the output of New and panics when an error occured

func NamespaceDNS added in v0.2.0

func NamespaceDNS() UUID

func NamespaceOID added in v0.2.0

func NamespaceOID() UUID

func NamespaceURL added in v0.2.0

func NamespaceURL() UUID

func NamespaceX500 added in v0.2.0

func NamespaceX500() UUID

func NewV1

func NewV1() (uuid UUID, err error)

func NewV2

func NewV2() (uuid UUID, err error)

func NewV3

func NewV3(ns UUID, name string) (uuid UUID, err error)

func NewV4

func NewV4() (uuid UUID, err error)

func NewV5

func NewV5(ns UUID, name string) (uuid UUID, err error)

func NewV6

func NewV6() (uuid UUID, err error)

func NewV7

func NewV7() (uuid UUID, err error)

func NewV8

func NewV8() (uuid UUID, err error)

func Parse

func Parse(str string) (UUID, error)

Parse parses a string as a UUID returning either the resulting UUID or an error

func ParseBytes

func ParseBytes(bytes []byte) (uuid UUID, err error)

ParseBytes parses a byte slice and returns the contained BINARY UUID or an error

func (UUID) IsMax

func (uuid UUID) IsMax() bool

IsMax returns true if the UUID contains only ones and is therefore invalid

func (UUID) IsNil

func (uuid UUID) IsNil() bool

IsNil returns true if the UUID contains only zeros and is therefore empty and invalid

func (UUID) MarshalBinary

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

MarshalBinary provides encoding.BinaryMarshaler

func (UUID) MarshalText

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

MarshalText provides encoding.TextMarshaler

func (*UUID) Scan

func (uuid *UUID) Scan(val interface{}) error

Scan provides database/sql.Scanner

func (UUID) String

func (uuid UUID) String() string

ToString returns the string representation of a UUID

func (UUID) Timestamp

func (uuid UUID) Timestamp() time.Time

Timestamp returns the timestamp of the UUID or nil if the UUID does not contain a timestamp

func (*UUID) UnmarshalBinary

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

UnmarshalBinary provides encoding.BinaryUnmarshaler

func (*UUID) UnmarshalText

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

UnmarshalText provides encoding.TextUnmarshaler

func (UUID) Value

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

Value provides database/sql/driver.Valuer

func (UUID) Version

func (uuid UUID) Version() int

Version returns the version of the UUID

Jump to

Keyboard shortcuts

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