Generate UUID v4 and v7 in Go
Copy-paste Go code that produces a UUID v4 (random, the long-standing default) or UUID v7 (time-ordered, the modern choice for database keys). Both blocks below are production-ready.
UUID v4 in Go
v4 is purely random — 122 bits of entropy, no structure. Use it when you don't want timing information embedded in the ID, or when you don't care about index locality.
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
id := uuid.New() // v4 by default
fmt.Println(id)
}
UUID v7 in Go
v7 embeds a millisecond Unix timestamp in the first 48 bits, so the IDs sort chronologically. This is what modern databases want from a primary key — sequential inserts land in adjacent index pages instead of scattering writes.
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
id, err := uuid.NewV7()
if err != nil {
panic(err)
}
fmt.Println(id)
}
Library support
`github.com/google/uuid` added `NewV7` in v1.6.0 (December 2023). It's the de facto Go UUID library — first-party Google fork, used by most modern Go services.
Notes
If you need a UUID type in a struct field (e.g. for pgx or sqlc), use `uuid.UUID`. For database scans, the library implements `sql.Scanner` and `driver.Valuer` out of the box.
Need them in bulk?
Our UUID generator produces 1 to 1000 UUIDs (v4 or v7) instantly, all in your browser, with one-click copy and JSON/CSV export. Useful for seeding test data or pre-generating IDs offline.