uuid

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package uuid provides support for generating and manipulating UUIDs.

See RFC 9562 for details.

Random components of new UUIDs are generated with a cryptographically secure random number generator.

UUIDs may be generated using various algorithms. The New function returns a new UUID generated using an algorithm suitable for most purposes.

Index

Constants

View Source
const UUIDLen = 36 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Length of a canonical UUID string.

Variables

View Source
var ErrInvalid = errors.New("uuid: invalid uuid")

Functions

This section is empty.

Types

type UUID

type UUID struct {
	Value [16]byte
}

A UUID is a Universally Unique Identifier as specified in RFC 9562.

UUIDs are comparable, such as with the == operator.

func Max

func Max() UUID

Max returns the Max UUID ffffffff-ffff-ffff-ffff-ffffffffffff.

The Max UUID is defined in Section 5.10 of RFC 9562.

func MustParse

func MustParse(s string) UUID

MustParse returns the UUID represented by s.

It panics if s is not a valid string representation of a UUID as defined by Parse.

func New

func New() UUID

New returns a new UUID.

Programs which do not have a need for a specific UUID generation algorithm should use New. At this time, New is equivalent to NewV4.

func NewV4

func NewV4() UUID

NewV4 returns a new version 4 UUID.

Version 4 UUIDs contain 122 bits of random data.

func NewV7

func NewV7() UUID

NewV7 returns a new version 7 UUID.

Version 7 UUIDs contain a timestamp in the most significant 48 bits, and at least 62 bits of random data.

func Nil

func Nil() UUID

Nil returns the Nil UUID 00000000-0000-0000-0000-000000000000.

The Nil UUID is defined in Section 5.9 of RFC 9562. Note that this is not the same as the Go value nil.

func Parse

func Parse(s string) (UUID, error)

Parse returns the UUID represented by s.

It accepts strings in the following forms:

f81d4fae-7dec-11d0-a765-00a0c91e6bf6
{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}
urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6
f81d4fae7dec11d0a76500a0c91e6bf6

Alphabetic characters in the hexadecimal digits may be any case.

func (UUID) AppendText

func (u UUID) AppendText(b []byte) ([]byte, error)

AppendText implements the encoding.TextAppender interface. The encoding is the same as returned by UUID.String. Requires at least UUIDLen bytes of spare capacity in buf. Always returns a nil error.

func (UUID) Compare

func (u UUID) Compare(v UUID) int

Compare compares the UUID u with v. If u is before v, it returns -1. If u is after v, it returns +1. If they are the same, it returns 0.

Compare uses the big-endian byte order defined in Section 6.11 of RFC 9562 for sorting.

func (UUID) Equal

func (u UUID) Equal(u2 UUID) bool

Equal reports whether u and u2 are the same UUID.

func (UUID) MarshalText

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

MarshalText implements the encoding.TextMarshaler interface. The encoding is the same as returned by UUID.String buf length must be at least UUIDLen bytes.

func (UUID) String

func (u UUID) String(buf []byte) string

String returns the string representation of u. It uses the lowercase hex-and-dash representation defined in RFC 9562. buf length must be at least UUIDLen bytes.

func (*UUID) UnmarshalText

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

UnmarshalText implements the encoding.TextUnmarshaler interface. The UUID is expected in a form accepted by Parse.

func (UUID) Version

func (u UUID) Version() int

Version returns the version number of u as defined in Section 4.2 of RFC 9562.

Jump to

Keyboard shortcuts

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