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
uuid.New()returns a random v4uuid.UUID— a fixed[16]bytearray, so it is comparable, copyable and has no heap allocation as a value.uuid.NewV7()(package v1.6.0 and later) returns a time-ordered v7 value together with an error, because the timestamp source can fail.uuid.UUID.String()renders the canonical hyphenated form.- For a Go service the interesting decision is which one to use in the schema. A random v4 key is fine as a unique identifier but hostile to a B-tree when rows arrive continuously; a v7 key sorts by creation time, so inserts append. Many Go teams keep both: v7 for the primary key, v4 for anything that must look unpredictable.
How to generate
- Press Generate UUID above to mint a value in this browser — nothing is sent anywhere.
- Copy one value with Copy, or set How many and use Copy all for a fixture list.
- Switch Version if your code needs v7 instead of the v4 default, and turn on Formatting to match your stack's conventions.
Use Cases
- Distributed service keys — minted in the application, so no round trip to the database is needed to learn the ID.
- Request and trace IDs in middleware, propagated through context.
- Idempotency keys for message handlers where redelivery is normal.
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
UUID v1 GeneratorUUID v3 GeneratorUUID v4 GeneratorUUID v5 GeneratorUUID v6 GeneratorUUID v7 GeneratorUUID v8 GeneratorBulk UUID GeneratorShort UUID GeneratorUUID ValidatorUUID FormatterJavaScript UUID GeneratorTypeScript UUID GeneratorNode.js UUID GeneratorReact UUID GeneratorPython UUID GeneratorJava UUID GeneratorC# UUID GeneratorGo UUID GeneratorPHP UUID GeneratorRuby UUID GeneratorRust UUID GeneratorKotlin UUID GeneratorSwift UUID GeneratorSQL UUID Generator