DevToolsForYou

UUID Generator in Go — Code Examples

UUID Generator in GoUse the online tool →

UUIDs (Universally Unique Identifiers) are 128-bit identifiers used as database primary keys, session tokens, and idempotency keys. UUID v4 (random) is the safe default; UUID v7 (time-ordered) is preferred for database primary keys.

Go's standard library does not include UUID generation. The google/uuid package is the standard choice.

Go (google/uuid)
// go get github.com/google/uuid
package main

import (
    "fmt"
    "github.com/google/uuid"
)

func main() {
    // UUID v4 — random
    id := uuid.New()
    fmt.Println(id.String()) // e.g. 110e8400-e29b-41d4-a716-446655440000

    // UUID v4 with error handling
    id2, err := uuid.NewRandom()
    if err != nil {
        panic(err)
    }
    fmt.Println(id2)

    // UUID v7 — time-ordered (google/uuid v1.6+)
    id7, _ := uuid.NewV7()
    fmt.Println(id7)

    // Parse a UUID string
    parsed, err := uuid.Parse("110e8400-e29b-41d4-a716-446655440000")
    if err != nil {
        panic(err)
    }
    fmt.Println(parsed.Version()) // 4
}
Notes & gotchas
  • uuid.New() panics on random source failure; use uuid.NewRandom() to handle the error explicitly.
  • UUID v7 support was added in google/uuid v1.6 — update your dependency if uuid.NewV7() is unavailable.
  • UUIDs implement encoding.TextMarshaler/Unmarshaler — they serialize correctly in JSON automatically.
Try it in your browser

Need to uuid generator without writing code? The UUID Generator runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open UUID Generator
UUID Generator in other languages