uuid for Go: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

uuid Library for Go: Complete Guide to UUID Generation

\n\n

1. What It Is and Why It's Needed

\n\n

UUID (Universally Unique Identifier) is an identification standard used in software development to create unique identifiers without the need for a centralized coordinator. In the Go ecosystem, the github.com/google/uuid library is the de facto standard for working with UUIDs. It provides reliable and performant tools for generating, parsing, and comparing UUIDs of various versions.

\n\n

Why is this library needed? In modern distributed systems, microservice architectures, and databases, it is often necessary to create unique identifiers that will not conflict across different system nodes. UUID solves this problem: you can generate identifiers on the client, server, or anywhere else without fear of collisions. The google/uuid library supports UUID versions 1 (time-based and MAC address), 4 (random), and 5 (SHA-1 hash-based).

\n\n

The library is actively used in projects such as Kubernetes, Docker, CockroachDB, and many others. It is optimized for high performance, supports thread-safe generation, and provides a convenient API for all major UUID operations.

\n\n

2. Installation

\n\n

To install the library, run the following command in your terminal:

\n\n
go get github.com/google/uuid
\n\n

After that, import the package in your code:

\n\n
import "github.com/google/uuid"
\n\n

Make sure you have Go version 1.16 or higher installed. If you are using Go modules (recommended), the library will automatically be added to your go.mod and go.sum files.

\n\n

3. Quick Start — Minimal Working Example

\n\n

Here is the simplest example of generating a UUID version 4 (random):

\n\n
package main\n\nimport (\n    "fmt"\n    "github.com/google/uuid"\n)\n\nfunc main() {\n    // Generate a new UUID version 4\n    id := uuid.New()\n    fmt.Println("Generated UUID:", id.String())\n    \n    // Verify that it is indeed a UUID\n    fmt.Println("UUID Type:", id.Version())\n    fmt.Println("UUID Variant:", id.Variant())\n}
\n\n

The program output will look something like this:

\n\n
Generated UUID: 6ba7b810-9dad-11d1-80b4-00c04fd430c8\nUUID Type: 4\nVariant: RFC 4122
\n\n

4. Main Methods and Functions

\n\n

4.1. uuid.New()

\n

Signature: func New() UUID

\n

Description: Generates a new UUID version 4 (random). This is the most popular method for creating unique identifiers. It uses a cryptographically secure random number generator.

\n\n
package main\n\nimport (\n    "fmt"\n    "github.com/google/uuid"\n)\n\nfunc main() {\n    id := uuid.New()\n    fmt.Printf("UUID: %s\\n", id.String())\n    fmt.Printf("Version: %d\\n", id.Version())\n    fmt.Printf("Variant: %d\\n", id.Variant())\n}
\n\n

4.2. uuid.NewUUID()

\n

Signature: func NewUUID() (UUID, error)

\n

Description: Generates a UUID version 1 based on the current time and MAC address. May return an error if the MAC address cannot be obtained.

\n\n
package main\n\nimport (\n    "fmt"\n    "log"\n    "github.com/google/uuid"\n)\n\nfunc main() {\n    id, err := uuid.NewUUID()\n    if err != nil {\n        log.Fatal("Error generating UUID:", err)\n    }\n    fmt.Printf("UUID v1: %s\\n", id.String())\n    fmt.Printf("Creation time: %v\\n", id.Time())\n    fmt.Printf("MAC address: %x\\n", id.Node())\n}
\n\n

4.3. uuid.NewRandom()

\n

Signature: func NewRandom() (UUID, error)

Recommendations