Categorygithub.com/gofrs/uuid/v5
modulepackage
5.3.1
Repository: https://github.com/gofrs/uuid.git
Documentation: pkg.go.dev

# README

UUID

License Build Status Go Reference Coverage Status Go Report Card CodeQL OpenSSF Best Practices OpenSSF Scorecard

Package uuid provides a pure Go implementation of Universally Unique Identifiers (UUID) variant as defined in RFC-9562. This package supports both the creation and parsing of UUIDs in different formats.

This package supports the following UUID versions:

  • Version 1, based on timestamp and MAC address
  • Version 3, based on MD5 hashing of a named value
  • Version 4, based on random numbers
  • Version 5, based on SHA-1 hashing of a named value
  • Version 6, a k-sortable id based on timestamp, and field-compatible with v1
  • Version 7, a k-sortable id based on timestamp

Project History

This project was originally forked from the github.com/satori/go.uuid repository after it appeared to be no longer maintained, while exhibiting critical flaws. We have decided to take over this project to ensure it receives regular maintenance for the benefit of the larger Go community.

We'd like to thank Maxim Bublis for his hard work on the original iteration of the package.

License

This source code of this package is released under the MIT License. Please see the LICENSE for the full content of the license.

Recommended Package Version

We recommend using v2.0.0+ of this package, as versions prior to 2.0.0 were created before our fork of the original package and have some known deficiencies.

Requirements

This package requires Go 1.19 or later

Usage

Here is a quick overview of how to use this package. For more detailed documentation, please see the GoDoc Page.

package main

import (
	"log"

	"github.com/gofrs/uuid/v5"
)

// Create a Version 4 UUID, panicking on error.
// Use this form to initialize package-level variables.
var u1 = uuid.Must(uuid.NewV4())

func main() {
	// Create a Version 4 UUID.
	u2, err := uuid.NewV4()
	if err != nil {
		log.Fatalf("failed to generate UUID: %v", err)
	}
	log.Printf("generated Version 4 UUID %v", u2)

	// Parse a UUID from a string.
	s := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
	u3, err := uuid.FromString(s)
	if err != nil {
		log.Fatalf("failed to parse UUID %q: %v", s, err)
	}
	log.Printf("successfully parsed UUID %v", u3)
}

References

# Functions

FromBytes returns a UUID generated from the raw byte slice input.
FromBytesOrNil returns a UUID generated from the raw byte slice input.
FromString returns a UUID parsed from the input string.
FromStringOrNil returns a UUID parsed from the input string.
Must is a helper that wraps a call to a function returning (UUID, error) and panics if the error is non-nil.
NewGen returns a new instance of Gen with some default values set.
NewGenWithHWAF builds a new UUID generator with the HWAddrFunc provided.
NewGenWithOptions returns a new instance of Gen with the options provided.
NewV1 returns a UUID based on the current timestamp and MAC address.
NewV1 returns a UUID based on the provided timestamp and MAC address.
NewV3 returns a UUID based on the MD5 hash of the namespace UUID and name.
NewV4 returns a randomly generated UUID.
NewV5 returns a UUID based on SHA-1 hash of the namespace UUID and name.
NewV6 returns a k-sortable UUID based on the current timestamp and 48 bits of pseudorandom data.
NewV6 returns a k-sortable UUID based on the provided timestamp and 48 bits of pseudorandom data.
NewV7 returns a k-sortable UUID based on the current millisecond-precision UNIX epoch and 74 bits of pseudorandom data.
NewV7 returns a k-sortable UUID based on the provided millisecond-precision UNIX epoch and 74 bits of pseudorandom data.
TimestampFromV1 returns the Timestamp embedded within a V1 UUID.
TimestampFromV6 returns the Timestamp embedded within a V6 UUID.
TimestampFromV7 returns the Timestamp embedded within a V7 UUID.
WithEpochFunc is a GenOption that allows you to provide your own EpochFunc function.
WithHWAddrFunc is a GenOption that allows you to provide your own HWAddrFunc function.
WithRandomReader is a GenOption that allows you to provide your own random reader.

# Constants

UUID DCE domains.
UUID DCE domains.
UUID DCE domains.
ErrIncorrectByteLength indicates the UUID byte slice length is invalid.
ErrIncorrectFormatInString can be returned instead of ErrInvalidFormat.
ErrIncorrectLength is returned when the UUID does not have the appropriate string length for parsing the UUID.
ErrInvalidFormat is returned when the UUID string representation does not match the expected format.
ErrInvalidVersion indicates an unsupported or invalid UUID version.
ErrNoHwAddressFound is returned when a hardware (MAC) address cannot be found for UUID generation.
ErrTypeConvertError is returned for type conversion operation fails.
Size of a UUID in bytes.
Version 1 (date-time and MAC address).
Version 3 (namespace name-based).
Version 4 (random).
Version 5 (namespace name-based).
Version 6 (k-sortable timestamp and random data, field-compatible with v1).
Version 7 (k-sortable timestamp and random data).
UUID layout variants.
UUID layout variants.
UUID layout variants.
Backward-compatible variant for RFC 4122.
UUID layout variants.

# Variables

DefaultGenerator is the default UUID Generator used by this package.
Max is the maximum UUID, as specified in RFC-9562, that has all 128 bits set to one.
Predefined namespace UUIDs.
Predefined namespace UUIDs.
Predefined namespace UUIDs.
Predefined namespace UUIDs.
Nil is the nil UUID, as specified in RFC-9562, that has all 128 bits set to zero.

# Structs

Gen is a reference UUID generator based on the specifications laid out in RFC-9562 and DCE 1.1: Authentication and Security Services.
NullUUID can be used with the standard sql package to represent a UUID value that can be NULL in the database.

# Interfaces

Generator provides an interface for generating UUIDs.

# Type aliases

EpochFunc is the function type used to provide the current time.
Error is a custom error type for UUID-related errors.
GenOption is a function type that can be used to configure a Gen generator.
HWAddrFunc is the function type used to provide hardware (MAC) addresses.
Timestamp is the count of 100-nanosecond intervals since 00:00:00.00, 15 October 1582 within a V1 or V6 UUID, or as a common intermediate representation of the (Unix Millisecond) timestamp within a V7 UUID.
UUID is an array type to represent the value of a UUID, as defined in RFC-9562.