Developer Documentation
Go
Updated September 19, 2026

Generate and Work with UUIDs in Go

Generate, parse, format, and validate UUIDs safely in Go.

UUIDs are 128-bit identifiers standardized by RFC 9562. UUIDv4 is random and broadly supported; newer UUID versions add other generation properties while keeping the same canonical textual shape.

Quick Start#

The following example uses the standard Go API or a widely used package for this task.

Quick start#

Go
1// go get github.com/google/uuid2import "github.com/google/uuid"34id := uuid.New()5fmt.Println(id.String())

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Parse input#

Go
1parsed, err := uuid.Parse(input)2if err != nil {3    return fmt.Errorf("invalid UUID: %w", err)4}5fmt.Println(parsed.String())

Important Notes#

Keep identifiers typed as uuid.UUID where possible instead of repeatedly parsing and formatting strings.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#