uuid

package
v0.0.0-...-571c105 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2025 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

This package implements a UUID generator and some other functionalities revolving UUIDs. Read in RFC9562 for more info about UUIDs.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadSliceLength = errors.New("uuid: slice does not has 16 bytes")
	ErrBadString      = errors.New("uuid: string could not be parsed correctly")
	ErrBadJSONString  = errors.New("uuid: slice is a malformed JSON string")
)

Functions

This section is empty.

Types

type UUID

type UUID [16]byte

This represents a 128bit UUID type. Elements of this type can, and should, be compared using the == operator. The zero value of UUID is classified as a Nil UUID according to RFC9562 section 5.9.

func FromBytes

func FromBytes(bytes []byte) (UUID, error)

Converts an UUID from a byte slice. The given byte slice should be of length 16, otherwise an error will be returned. Due to this, this function is NOT interchangeable with FromString, a byte slice that represents the string representation of an UUID should be converted with FromString.

func FromString

func FromString(str string) (UUID, error)

Converts an UUID from a string format. Note that this function is NOT interchangeable with FromBytes, even considering convertion.

func NewUUIDv7

func NewUUIDv7() UUID

Generates a new UUID accourding to version 7. It's safe to call this function from multiple goroutines.

The memory layout of a UUIDv7 is as follows:

  • Unix Timestamp: 48-bit big-endian unsigned number of the Unix Epoch timestamp in milliseconds. Occupies bits 0 through 47, octets 1 through 5.

  • Version: 4-bit version field, set to 0b0111 (7). Occupies bits 48 through 51, octet 6.

  • Random A: 12-bit pseudorandom data to provide uniqueness. Occupies bits 52 through 63, octects 6 through 7.

  • Variant: 2-bit variant field, set to 0b10. Occupies bits 64 through 65, octet 8.

  • Random B: 62-bit pseudorandom data to provide uniqueness. Occupies bits 66 through 127, octets 8 through 15.

func (UUID) IsNil

func (uuid UUID) IsNil() bool

Verifies whether the given UUID is the Nil UUID. Not be confused with a nil pointer to an UUID. Equivalent to using == against the zero value of this type.

func (UUID) MarshalJSON

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

Implements the interface [json.Marshaler] on the UUID type.

func (UUID) String

func (uuid UUID) String() string

Implements the interface fmt.Stringer on the UUID type.

func (*UUID) UnmarshalJSON

func (uuid *UUID) UnmarshalJSON(buf []byte) error

Implements the interface [json.Unmarshaler] on the UUID type. The given byte slice should be a valid JSON string literal.

Jump to

Keyboard shortcuts

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