UUID in Go

Go UUID Generator

Go has no UUID type in the standard library, and the de-facto answer is github.com/google/uuid — small, maintained, and now with v7.

google/uuiduuid.New() v4NewV7()

Generated locally with the Web Crypto API — nothing leaves your browser.

Generating UUIDs in Go

How to generate

  1. Press Generate UUID above to mint a value in this browser — nothing is sent anywhere.
  2. Copy one value with Copy, or set How many and use Copy all for a fixture list.
  3. Switch Version if your code needs v7 instead of the v4 default, and turn on Formatting to match your stack's conventions.

Use Cases

How Go UUID Generator Compares

uuid.New()v4, random, allocation-free as a value
uuid.NewV7()v7, time-ordered, returns an error
uuid.NewRandom()Same as New() but can fail — use when you want to handle the error

Code Examples

Go
package main

import (
	"fmt"

	"github.com/google/uuid"
)

func main() {
	v4 := uuid.New()                 // random
	v7, err := uuid.NewV7()          // time-ordered
	if err != nil {
		panic(err)
	}

	fmt.Println(v4.String())         // 4de2b1c0-8a7f-4e31-9c2d-1f0a6b7c8d9e
	fmt.Println(v7.String())         // 0198c2f1-6d8a-7cc3-9b21-4a7f0e5d3c11
}
Parsing and NULL
id, err := uuid.Parse("4de2b1c0-8a7f-4e31-9c2d-1f0a6b7c8d9e")
if err != nil {
	// malformed input
}

// Optional column value
nullable := uuid.NullUUID{UUID: id, Valid: true}

Frequently Asked Questions

Does Go have a UUID in the standard library?
No. crypto/rand gives you the randomness, but the UUID layout comes from a package — github.com/google/uuid is the most widely used.
How do I generate UUID v7 in Go?
uuid.NewV7() from google/uuid v1.6.0 or later. It returns (uuid.UUID, error), since the timestamp source can fail.
Is uuid.UUID a string in Go?
No — it is a [16]byte array, which makes it comparable and map-key friendly. Use .String() at the API or database boundary.

Related tools