uuid Library for Go: Complete Guide to UUID Generation
\n\n1. What It Is and Why It's Needed
\n\nUUID (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.
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).
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\n2. Installation
\n\nTo install the library, run the following command in your terminal:
\n\ngo get github.com/google/uuid\n\nAfter that, import the package in your code:
\n\nimport "github.com/google/uuid"\n\nMake 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.
3. Quick Start — Minimal Working Example
\n\nHere is the simplest example of generating a UUID version 4 (random):
\n\npackage 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\nThe program output will look something like this:
\n\nGenerated UUID: 6ba7b810-9dad-11d1-80b4-00c04fd430c8\nUUID Type: 4\nVariant: RFC 4122\n\n4. Main Methods and Functions
\n\n4.1. uuid.New()
\nSignature: func New() UUID
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\npackage 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\n4.2. uuid.NewUUID()
\nSignature: func NewUUID() (UUID, error)
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\npackage 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\n4.3. uuid.NewRandom()
\nSignature: func NewRandom() (UUID, error)
Also in library
OAuth Client for TypeScript: Integrating Authorization in Web Applications
Geolocation and Maps in C++: Overview of Libraries and Tools
Spectre.Console for C# (.NET): Complete Guide with Examples
gRPC for Kotlin: Libraries, Server, and Client — Code Examples