Complete reference for generating UUIDs in Go (Golang) — covers the google/uuid package, the de facto Golang UUID library, validation, and GORM/database integration.
Quick Start
Generate a Golang UUID (v4) with google/uuid
Whether you call it Go or Golang, developers searching for a Golang UUID solution consistently land on the same package: google/uuid. It has become the unofficial standard for Golang UUID generation because it is maintained directly by Google, receives regular updates, and integrates cleanly with popular Golang frameworks like Gin, Echo, and GORM.
Go’s standard library has no UUID package. google/uuid is the de facto standard, maintained by Google and used across the Go (Golang) ecosystem, including the official Go documentation examples:
Terminal
go get github.com/google/uuid
Go
package main
import (
“fmt”
“github.com/google/uuid”
)
func main() {
id := uuid.New()
fmt.Println(id.String())
// 550e8400-e29b-41d4-a716-446655440000
}
💡
uuid.New() vs uuid.NewRandom()
uuid.New() is a convenience wrapper around uuid.NewRandom() that panics on error instead of returning one. Use NewRandom() directly if you want to handle the (extremely rare) entropy-source error yourself.
All Versions
UUID Versions with google/uuid
Version
Go Call
Use Case
v1 (Time-based)
uuid.NewUUID()
Timestamp + MAC address. Legacy systems only.
v3 (MD5 name-based)
uuid.NewMD5(ns, data)
Deterministic. Use v5 instead (MD5 is broken).
v4 (Random)
uuid.New()
General purpose. Default choice.
v5 (SHA-1 name-based)
uuid.NewSHA1(ns, data)
Deterministic — same input always gives same UUID.
v7 (Unix timestamp)
uuid.NewV7() (v1.4+)
Time-ordered. Recommended for database primary keys in 2026.
import (
“github.com/google/uuid”
“gorm.io/gorm”
)
type Order struct {
ID uuid.UUID `gorm:”type:uuid;primaryKey;default:gen_random_uuid()”`
CreatedAt time.Time
}
JSON marshaling (uuid.UUID implements Stringer)
type Response struct {
ID uuid.UUID `json:”id”`
}
// Marshals automatically to the standard string format
json.NewEncoder(w).Encode(Response{ID: uuid.New()})
FAQ
Go UUID FAQ
No. Go’s standard library does not include UUID generation. The github.com/google/uuid package, maintained by Google, is the de facto standard used across the Go ecosystem, including by GORM and most database drivers.
uuid.New() is a convenience function that calls NewRandom() internally and panics if an error occurs (which is extremely rare, tied to the system entropy source). uuid.NewRandom() returns the UUID and an error separately, letting you handle the error explicitly. Most applications use uuid.New() since the error case is virtually never triggered in practice.
Install google/uuid version 1.4.0 or later, then call uuid.NewV7(). This returns a time-ordered UUID with a Unix millisecond timestamp prefix and an error value to check. UUID v7 is recommended for database primary keys in 2026 because it improves B-tree index performance compared to fully random UUID v4 keys.
Yes. The google/uuid package’s UUID type implements the Stringer, MarshalText, and UnmarshalText interfaces, so it serializes to and from JSON as the standard hyphenated string format automatically — no custom marshaling code needed in your structs.