uuid

package module
v0.0.0-...-041b57f Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 10 Imported by: 0

README

Go UUID

Pipeline

A library that provides a way to handle, and generate UUIDs. Complies with the RFC 4122 standard. based the code on what I wrote for .dotnet - DaanV2.UUID.Net

import "github.com/DaanV2/go-uuid"

u := uuid.New()
u := uuid.NewString()

//Specific version
u := uuid.V4.New()
u := uuid.V4.NewString()

u := uuid.V5.New([]byte("Some data"))
u := uuid.V3.New([]byte("Some data"))

// Batch generation
u := uuid.V4.NewBatch(10)

Documentation

Index

Constants

View Source
const (
	TOTAL_BITS    = 128
	TOTAL_BYTES   = TOTAL_BITS / int(unsafe.Sizeof(uint8(0))*8)
	STRING_LENGTH = TOTAL_BYTES*2 + 4

	VERSION_BYTE_INDEX = 6
	VARIANT_BYTE_INDEX = 8
)
View Source
const (
	// VERSION_BYTE_MASK is the mask for the version byte.
	VERSION_BYTE_MASK uint8 = 0b11110000

	// V1 is the version that is made from the timestamp and MACAddress
	Version1 Version = 0b0001_0000
	// V2 Not Implemented
	Version2 Version = 0b0010_0000
	// V3 is the version that hashes (MD5) given data into an UUID
	Version3 Version = 0b0011_0000
	// V4 is the version that generates a random UUID
	Version4 Version = 0b0100_0000
	// V5 is the version that hashes (SHA1) given data into an UUID
	Version5 Version = 0b0101_0000
)

Variables

View Source
var (
	// ErrInvalidVariant is returned when the variant is invalid.
	ErrInvalidVariant = errors.New("invalid UUID variant")
)
View Source
var (
	// ErrInvalidVersion is returned when the version is invalid.
	ErrInvalidVersion = errors.New("invalid UUID version")
)
View Source
var (
	// ErrMacAddrNotFound is returned when the mac address is not found
	ErrMacAddrNotFound = errors.New("mac address not found")
)
View Source
var Nil = Empty()

Nil is the nil UUID (00000000-0000-0000-0000-000000000000).

View Source
var V1 v1
View Source
var V3 v3

V3 is a UUID version 3 generator, using MD5 hashing

View Source
var V4 v4

V4 is a UUID version 4 generator

View Source
var V5 v5

V5 is a UUID version 4 generator, using SHA-1 hashing

Functions

func IsValidStringUUID

func IsValidStringUUID(s string) bool

IsValidStringUUID returns true if the string is a valid UUID.

func NewHex

func NewHex() string

NewHex returns a new UUID version 4 as a hex string

func NewString

func NewString() string

NewString returns a new UUID version 4 as a string

func ValidateStringUUID

func ValidateStringUUID(s string) error

ValidateStringUUID returns an error if the string is not a valid UUID.

Types

type UUID

type UUID [16]byte

UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC-4122.

func Empty

func Empty() UUID

Empty returns an empty UUID (00000000-0000-0000-0000-000000000000).

func FromBytes

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

FromBytes returns a UUID from a byte slice.

func New

func New() UUID

New returns a new UUID version 4

func StampVersionVariant

func StampVersionVariant(uuid UUID, version Version, variant Variant) UUID

StampVersion stamps the version and variant of the UUID.

func (UUID) Bytes

func (u UUID) Bytes() [16]byte

Bytes returns the UUID as a copied byte slice.

func (UUID) IsEmpty

func (u UUID) IsEmpty() bool

IsEmpty returns true if the UUID is empty (00000000-0000-0000-0000-000000000000).

func (UUID) String

func (u UUID) String() string

String returns a string representation of the UUID in the following format: 00000000-0000-0000-0000-000000000000

func (UUID) StringHex

func (u UUID) StringHex() string

StringHex returns a hex string representation of the UUID.

func (UUID) Variant

func (u UUID) Variant() Variant

Variant returns the variant of the UUID.

func (UUID) Version

func (u UUID) Version() Version

Version returns the version of the UUID.

type Variant

type Variant uint

Variant represents the variant of the UUID.

const (
	// Variant0 is reserved
	Variant0 Variant = 0b0000_0000
	// Variant1 is the RFC 4122 variant
	Variant1 Variant = 0b1000_0000
	// Variant2 is reserved
	Variant2 Variant = 0b1100_0000
	// Variant3 is future reserved
	Variant3 Variant = 0b1110_0000
)

func VariantFromValue

func VariantFromValue(value int) (Variant, error)

VariantFromValue returns the variant of the UUID from the value, returns ErrInvalidVariant if the value is invalid.

func (Variant) String

func (v Variant) String() string

String returns the string representation of the variant.

func (Variant) Value

func (v Variant) Value() int

Value returns the value of the variant.

type Version

type Version uint

Version represents the version of the UUID.

func VersionFromValue

func VersionFromValue(value int) (Version, error)

VersionFromValue returns the version of the UUID from the value, returns ErrInvalidVersion if the value is invalid.

func (Version) String

func (v Version) String() string

String returns the string representation of the version.

func (Version) Value

func (v Version) Value() int

Value returns the value of the version.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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