How to Generate UUIDs in Go

Generate UUIDs in Go with a package such as github.com/google/uuid because the standard library does not provide UUID generation.

Recommendation: pick one UUID package and standardize on it early. That keeps both UUID v4 and UUID v7 generation consistent across services.

How to generate UUID v4 in Go

import (
    "fmt"
    "github.com/google/uuid"
)

func main() {
    id := uuid.New()
    fmt.Println(id.String())
}

How to generate UUID v7 in Go

import (
    "fmt"
    "github.com/google/uuid"
)

func main() {
    id, err := uuid.NewV7()
    if err != nil {
        panic(err)
    }

    fmt.Println(id.String())
}

Native support notes

  • Go does not provide UUID generation in the standard library.
  • Package choice matters because UUID v7 support varies by library version.
  • Document the chosen package so internal services all generate the same format.

Practical notes

  • UUID v4 is good for simple distributed ID generation.
  • UUID v7 is often better for append-heavy tables and sorted event streams.
  • Read database performance guidance before using random UUIDs as clustered keys.

Frequently Asked Questions

No. In Go, UUID generation normally comes from an external package.

It reduces drift in behavior, dependencies, and version support across services and CLI tools. That matters even more for UUID v7, because functions such as uuid.NewV7() depend on the package version you ship.

Learn more

These articles expand on related concepts, formats and practical considerations.

By using this site, you agree to our Privacy Policy and Terms of Service. You are not permitted to use the GUIDs (also known as UUIDs) generated by this site or use any other content, services and information available if you do not agree to these terms.
Disclaimer: All information is provided for general educational and technical reference only. While we aim to keep the content accurate, current and aligned with published standards, no guarantees are made regarding completeness, correctness or suitability for any specific use case.
GUID specifications, best practices, security guidance, database behavior and ecosystem conventions (including cloud platforms and identifier formats) may change over time or differ by implementation. Examples, recommendations and comparisons are illustrative and may not apply universally.
This content should not be considered legal, security, compliance or architectural advice. Before making critical design, security or production decisions, always consult the latest official standards and vendor-specific documentation.
Always evaluate behavior in your own environment.
Standards Compliance: The GUIDs generated by this site conform to RFC 4122 and RFC 9562 specifications whenever possible, using cryptographically secure random number generation.