Developer Documentation
Go
Updated September 19, 2026

Generate Nano IDs in Go

Generate compact URL-friendly random identifiers with Nano ID libraries in Go.

Nano ID is a compact random identifier format commonly used when shorter URL-friendly IDs are preferable to canonical UUID strings. Use a cryptographically secure implementation rather than Math.random-style generators.

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/matoous/go-nanoid/v22package main34import (5    "fmt"6    gonanoid "github.com/matoous/go-nanoid/v2"7)89func main() {10    id, err := gonanoid.New()11    if err != nil { panic(err) }12    fmt.Println(id)13}

Common Operations#

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

Custom alphabet#

Go
1id, err := gonanoid.Generate("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", )2if err != nil {3    return err4}5fmt.Println(id)

Important Notes#

Propagate generator errors instead of silently falling back to a weaker randomness source.

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#

Implement NanoID in other languages: