uuid

package
v1.2.9 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2025 License: MIT Imports: 7 Imported by: 3

README

UUID Package

The uuid package provides functionality for generating and handling universally unique identifiers (UUIDs) in Go. This package supports multiple versions of UUIDs, including version 1, 2, 3, and 4.

Installation

To install the package, use the following command:

go get github.com/nandlabs/golly/uuid


Usage

Generating UUIDs

The uuid package provides functions to generate different versions of UUIDs.

Version 1 UUID

Version 1 UUIDs are based on the current timestamp and the MAC address of the machine.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/uuid"
)

func main() {
    u, err := uuid.V1()
    if err != nil {
        fmt.Println("Error generating UUID:", err)
        return
    }
    fmt.Println("Version 1 UUID:", u.String())
}
Version 2 UUID

Version 2 UUIDs are based on the MAC address, process ID, and current timestamp.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/uuid"
)

func main() {
    u, err := uuid.V2()
    if err != nil {
        fmt.Println("Error generating UUID:", err)
        return
    }
    fmt.Println("Version 2 UUID:", u.String())
}
Version 3 UUID

Version 3 UUIDs are based on a namespace and a name, using MD5 hashing.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/uuid"
)

func main() {
    namespace := "example.com"
    name := "example"
    u, err := uuid.V3(namespace, name)
    if err != nil {
        fmt.Println("Error generating UUID:", err)
        return
    }
    fmt.Println("Version 3 UUID:", u.String())
}
Version 4 UUID

Version 4 UUIDs are randomly generated.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/uuid"
)

func main() {
    u, err := uuid.V4()
    if err != nil {
        fmt.Println("Error generating UUID:", err)
        return
    }
    fmt.Println("Version 4 UUID:", u.String())
}
Parsing UUIDs

The ParseUUID function allows you to parse a UUID string into a UUID object.

package main

import (
    "fmt"
    "github.com/nandlabs/golly/uuid"
)

func main() {
    uuidStr := "123e4567-e89b-12d3-a456-426655440000"
    u, err := uuid.ParseUUID(uuidStr)
    if err != nil {
        fmt.Println("Error parsing UUID:", err)
        return
    }
    fmt.Println("Parsed UUID:", u.String())
}

Documentation

Overview

package uuid provides a set of utilities for working with UUIDs in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type UUID

type UUID struct {
	// contains filtered or unexported fields
}

UUID represents a universally unique identifier.

func ParseUUID

func ParseUUID(s string) (u *UUID, err error)

ParseUUID parses a UUID string.

func V1

func V1() (u *UUID, err error)

V1 generates a version 1 UUID.

func V2

func V2() (u *UUID, err error)

V2 generates a version 2 UUID.

func V3

func V3(namespace string, name string) (u *UUID, err error)

V3 generates a version 3 UUID.

func V4

func V4() (u *UUID, err error)

V4 generates a version 4 UUID.

func (*UUID) Bytes

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

Bytes returns the bytes of the UUID.

func (*UUID) String

func (u *UUID) String() string

String returns the string representation of the UUID.

Jump to

Keyboard shortcuts

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