uuid

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2025 License: BSL-1.0 Imports: 13 Imported by: 1

README

UUID

Current release Documentation

This package implements the UUID versions defined in RFC9562.

Versions 1 through 5 are unchanged from 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 and drops the node id for random data making it unguessable and more collision resistant, 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

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 fmt.Stringer, 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 and print in the canonical format by default.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetMACAddress added in v1.0.0

func SetMACAddress(macAddr net.HardwareAddr) error

SetMACAddress sets the MAC address to be used for generating UUIDs. The MAC address must be 6 bytes long. If the MAC address is not set, a random MAC address will be generated. WARNING: This function is not thread-safe. Make sure to set the MAC address before generating any UUIDs.

func UseHardwareMAC added in v0.2.0

func UseHardwareMAC() error

UseHardwareMAC sets the MAC address to be used for generating UUIDs to the first valid hardware MAC address found on the system. If no valid hardware MAC address is found, an error is returned. WARNING: This function is not thread-safe. Make sure to set the MAC address before generating any UUIDs.

Types

type UUID

type UUID [16]byte

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

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)

NewV1 returns a new UUID based on the current timestamp and MAC address. The timestamp is retrieved from the system clock. The MAC address is randomly generated at application startup and can be overridden using SetMACAddress or UseHardwareMAC.

func NewV3

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

NewV3 returns a new UUID based on the MD5 hash of the provided namespace and name.

func NewV4

func NewV4() (uuid UUID)

NewV4 returns a new UUID generated from cryptographically secure random data.

func NewV5

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

NewV5 returns a new UUID based on the SHA-1 hash of the provided namespace and name.

func NewV6

func NewV6() (uuid UUID)

NewV6 returns a new UUID based on the current timestamp and MAC address. The timestamp is retrieved from the system clock. The MAC address is randomly generated at application startup and can be overridden using SetMACAddress or UseHardwareMAC. Unlike UUIDv1, UUIDv6 is designed to be sortable by time using binary or lexicographical comparison.

func NewV7

func NewV7() (uuid UUID)

NewV7 returns a new UUID based on the current timestamp and random data. The timestamp is retrieved from the system clock. The random data is generated using the cryptographically secure random number generator. This implementation uses the fractional millisecond approach for ordering of UUIDs within the same millisecond.

func NewV8

func NewV8(data []byte) (uuid UUID)

NewV8 returns a new UUID based on the provided data. The data must be 16 bytes long. Bits 48-51 of the UUID are set to 0b1000 (version 8). Bits 64-65 are set to 0b10 (variant RFC 4122/RFC 9562).

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 UUID or an error The byte slice can be either in binary format (16 bytes) or in string format (36 bytes)

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 any) 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