Go · google/uuid · Code Reference
Generate a UUID in Go
Complete reference for generating UUIDs in Go — covers the google/uuid package (the Go standard), validation, and GORM/database integration.
Quick Start
Generate a Random UUID (v4) with google/uuid
Go’s standard library has no UUID package. google/uuid is the de facto standard, maintained by Google and used across the Go ecosystem:
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. |
Go — UUID v7 for database keys
id, err := uuid.NewV7()
if err != nil {
log.Fatal(err)
}
fmt.Println(id.String()) // time-ordered, sortable UUID
Validation
Validate a UUID String in Go
Go — with google/uuid
import “github.com/google/uuid”
func isValidUUID(s string) bool {
_, err := uuid.Parse(s)
return err == nil
}
isValidUUID(“550e8400-e29b-41d4-a716-446655440000”) // true
isValidUUID(“not-a-uuid”) // false
Go — regex (no dependency)
import “regexp”
var uuidRegex = regexp.MustCompile(
`^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`,
)
func isValidUUID(s string) bool {
return uuidRegex.MatchString(strings.ToLower(s))
}
Frameworks
UUIDs in GORM & PostgreSQL
GORM model with UUID primary key
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.
Related Tools
Need to Generate a UUID Right Now?
Skip the code — use our free browser-based generator for all 8 UUID versions.
Open UUID Generator