Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

UUID

The uuid module creates and validates UUID values using github.com/google/uuid. UUIDs are a distinct Goblin type; converting one to a string produces its canonical lowercase representation.

import "uuid"

var id = uuid.new()
print(id)
print(uuid.is_valid("550e8400-e29b-41d4-a716-446655440000"))

var stable_id = uuid.new(
    version=5,
    namespace=uuid.NAMESPACE_DNS,
    data="example.com",
)

API

FunctionDescription
UUID(value)Constructs a UUID from a UUID, string, or 16-byte Bytes value. Raises ParseError when invalid.
new(version=4, namespace=nil, data=nil)Creates a UUID of version 1, 3, 4, 5, 6, or 7.
is_valid(value)Returns whether a string is a valid UUID representation.

new() defaults to a random version 4 UUID. Versions 3 and 5 require both a UUID namespace and data; data may be a string (encoded as UTF-8) or Bytes. Those arguments are rejected for all other versions. The predefined namespaces are NAMESPACE_DNS, NAMESPACE_URL, NAMESPACE_OID, and NAMESPACE_X500.

UUID values expose these attributes:

AttributeDescription
bytesThe UUID's 16 raw bytes.
urnThe UUID in urn:uuid:... form.
versionThe numeric UUID version.
variantThe UUID variant name.
timeCreation time as a Time, for versions 1, 6, and 7.
clock_sequenceClock sequence, for version 1.
nodeSix node bytes, for versions 1 and 6.

Accessing an attribute that is not defined for the UUID's version raises ValueError. Functions accept both positional and keyword arguments; is_valid() requires a string.