uuid

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2025 License: MIT Imports: 4 Imported by: 1

README

Go UUID

A Go package for generating and parsing RFC 4122 compliant UUIDs (Universally Unique Identifiers).

Features

  • Generate RFC 4122 version 4 UUIDs using cryptographically secure random numbers
  • Parse UUID strings into UUID objects
  • Convert UUIDs to standard string representation
  • Comprehensive error handling
  • Zero dependencies outside of Go standard library

Installation

go get github.com/Aj4x/uuid

Requires Go 1.23.0 or later.

Usage

Import the package
import "github.com/Aj4x/uuid"
Generate a new UUID
// Generate a new random UUID
id, err := uuid.NewUUID()
if err != nil {
    // Handle error
    log.Fatal(err)
}

// Convert UUID to string
fmt.Println(id.String()) // e.g., "331495aa-cdef-4042-8123-aabbccddeeff"
Parse a UUID string
// Parse a UUID from string
id, err := uuid.ParseUUID("331495aa-cdef-4042-8123-aabbccddeeff")
if err != nil {
    // Handle error
    log.Fatal(err)
}

// Use the UUID
fmt.Println(id)
Error Handling

The package provides specific error types for different validation failures:

// Check for specific error types
id, err := uuid.ParseUUID("invalid-uuid")
if errors.Is(err, uuid.ErrInvalidUUIDFormat) {
    fmt.Println("The UUID format is invalid")
}

Available error types:

  • ErrFailedToGenerateUUID: Failed to generate a UUID
  • ErrInvalidUUIDFormat: Invalid UUID format
  • ErrInvalidUUIDLength: Invalid UUID length
  • ErrInvalidUUIDHex: Invalid UUID hex character
  • ErrFailedToDecodeUUID: Failed to decode UUID

License

MIT License

Copyright (c) 2025 Aj4x

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error string
const (
	ErrFailedToGenerateUUID Error = "failed to generate UUID"
	ErrInvalidUUIDFormat    Error = "invalid UUID format"
	ErrInvalidUUIDLength    Error = "invalid UUID length"
	ErrInvalidUUIDHex       Error = "invalid UUID hex character"
	ErrFailedToDecodeUUID   Error = "failed to decode UUID"
)

func (Error) Error

func (e Error) Error() string

type UUID

type UUID [16]byte

UUID represents a universally unique identifier (UUID) as defined by RFC 4122. It is a 128-bit (16-byte) array, typically used for globally unique identification. The UUID is often represented as a 36-character string with hexadecimal digits and hyphens. UUIDs are used to uniquely identify objects or entities in distributed systems or databases.

func NewUUID

func NewUUID() (UUID, error)

NewUUID generates a new RFC4122 version 4 UUID using a cryptographic random source and returns it along with any error encountered during generation.

func ParseUUID

func ParseUUID(s string) (UUID, error)

ParseUUID parses a UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" and returns the corresponding UUID object.

func (UUID) String

func (g UUID) String() string

String converts the UUID to its standard string representation, formatted as 8-4-4-4-12 hexadecimal characters separated by dashes.

Jump to

Keyboard shortcuts

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