Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/uuids/go$ _

Generate UUIDs in Go

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: Go

What: Generate UUIDs in Go using the <code>google/uuid</code> package. Most popular UUID library for Go with support for UUID v1, v4, v5, and v7.

Try it: Use our interactive Uuids generator or integrate this code into your Go application.

Generate UUIDs in Go using the google/uuid package. Most popular UUID library for Go with support for UUID v1, v4, v5, and v7. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C# , C++ and Ruby or use our interactive web generator.

Go Code Example

package main

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

func main() {
    // UUID v4 (random)
    uuid4 := uuid.New()
    fmt.Println(uuid4.String())  // Example: 3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f

    // UUID v7 (timestamp-ordered, best for databases)
    uuid7, err := uuid.NewV7()
    if err != nil {
        panic(err)
    }
    fmt.Println(uuid7.String())  // Example: 018e5e1a-7b2c-7d4f-9a3e-8c7f6e5d4c3b

    // UUID v5 (namespaced)
    namespace := uuid.NameSpaceDNS
    name := "example.com"
    uuid5 := uuid.NewSHA1(namespace, []byte(name))
    fmt.Println(uuid5.String())  // Always same: cfbff0d1-9375-5685-968c-48ce8b15ae17

    // Parse UUID from string
    uuidString := "3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f"
    parsed, err := uuid.Parse(uuidString)
    if err != nil {
        fmt.Println("Invalid UUID")
    }
    fmt.Println("Version:", parsed.Version())  // Outputs: 4

    // Generate multiple UUIDs
    for i := 0; i < 5; i++ {
        fmt.Println(uuid.New())
    }

    // Nil UUID
    nilUUID := uuid.Nil
    fmt.Println(nilUUID)  // 00000000-0000-0000-0000-000000000000
}

// Install: go get github.com/google/uuid

[EXPLANATION]

Install with go get github.com/google/uuid. uuid.New() generates UUID v4. uuid.NewV7() generates timestamp-ordered UUIDs ideal for database primary keys. The package is maintained by Google.

Expected Output

3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f
018e5e1a-7b2c-7d4f-9a3e-8c7f6e5d4c3b
cfbff0d1-9375-5685-968c-48ce8b15ae17
4
00000000-0000-0000-0000-000000000000

Common Use Cases

  • gRPC service request IDs
  • Kubernetes resource identifiers
  • Microservices correlation IDs
  • PostgreSQL UUID primary keys
  • Distributed tracing span IDs
  • Event sourcing event IDs

Important Notes

  • google/uuid is the most popular Go UUID package
  • UUID v7 is recommended for database keys (sortable)
  • Package is thread-safe
  • Use uuid.Nil for zero/null UUID checks

Try Our Interactive Generator

Don't want to write code? Use our free web-based Uuids generator with instant results.

TRY UUIDS GENERATOR →