Help us shape the future of Go! Take the 2026 Go Developer Survey

uuid

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package uuid is a stdlib-only v4 UUID: type UUID [16]byte, New (v4 via crypto/rand), strict Parse/MustParse (8-4-4-4-12, errors.Is-able sentinel), String, IsZero, and MarshalText/UnmarshalText.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GenerateError

type GenerateError struct{ Err error }

GenerateError reports a failure to read randomness while generating a UUID. It wraps the underlying reader error so callers can errors.As to a *GenerateError and errors.Unwrap (or read .Err) to inspect the cause.

func (*GenerateError) Error

func (e *GenerateError) Error() string

func (*GenerateError) Unwrap

func (e *GenerateError) Unwrap() error

type ParseError

type ParseError struct {
	Input string
	Err   error
}

ParseError reports that a string was not a valid canonical UUID encoding. It carries the offending Input (always a UUID-sized string, so including it is bounded) and wraps the leaf sentinel errInvalidText as Err, which callers can reach via errors.As to a *ParseError or errors.Unwrap / errors.Is.

func (*ParseError) Error

func (e *ParseError) Error() string

func (*ParseError) Unwrap

func (e *ParseError) Unwrap() error

type UUID

type UUID [16]byte

UUID is a 128-bit identifier stored as a raw 16-byte array.

func MustParse

func MustParse(s string) UUID

MustParse is Parse for pinned, package-level constant IDs: it returns the decoded UUID or panics on a parse error. Use it only with compile-time literals you control, never with external input.

func New

func New() (UUID, error)

New returns a version-4 (random) UUID sourced from crypto/rand. It returns a *GenerateError if the randomness source cannot supply 16 bytes.

func Parse

func Parse(s string) (UUID, error)

Parse decodes the canonical 8-4-4-4-12 hyphenated form into a UUID. Hyphens must sit at their fixed offsets and the remaining positions must be hex digits; hex is case-insensitive, and the decoded value is normalized so String/MarshalText always re-emit lowercase. Any structural or hex failure yields the zero UUID and a *ParseError wrapping errInvalidText (fail-secure: no partial value on error).

func (UUID) IsZero

func (u UUID) IsZero() bool

IsZero reports whether u is the zero UUID (absent / root).

func (UUID) MarshalText

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

MarshalText encodes u as its canonical 8-4-4-4-12 hyphenated form, so JSON (and any other encoding.TextMarshaler consumer) emits a readable string rather than a 16-int byte array.

func (UUID) String

func (u UUID) String() string

String returns the canonical lowercase 8-4-4-4-12 hyphenated hex encoding.

func (*UUID) UnmarshalText

func (u *UUID) UnmarshalText(text []byte) error

UnmarshalText parses the 8-4-4-4-12 hyphenated form back into the 16 bytes by delegating to Parse (the single parsing implementation). The input must have hyphens at the fixed offsets and hex digits elsewhere; hex digits are case-insensitive (both 0x0a and 0x0A decode to 0x0a). The decoded value is normalized, so String/MarshalText always re-emit lowercase. Structurally invalid input returns a *ParseError (which unwraps to errInvalidText) and leaves the receiver unchanged (no partial write).

Jump to

Keyboard shortcuts

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