Appearance
Generate UUID in Go
The most popular way to generate UUIDs in Go is using the google/uuid package. Here's how to generate a random UUID version 4:
go
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
// Generate UUID version 4 - random
id := uuid.New()
fmt.Printf("Generated UUID: %s\n", id.String())
}
For generating specific UUID versions, the google/uuid package provides comprehensive support:
go
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
// Generate UUID version 1 - time based
uuid1, err := uuid.NewUUID()
if err != nil {
panic(err)
}
fmt.Printf("UUID v1: %s\n", uuid1.String())
// Generate UUID version 3 - deterministic and hashed with MD5
namespace := uuid.NameSpaceURL
uuid3 := uuid.NewMD5(namespace, []byte("https://example.com"))
fmt.Printf("UUID v3: %s\n", uuid3.String())
// Generate UUID version 4 - random
uuid4 := uuid.New()
fmt.Printf("UUID v4: %s\n", uuid4.String())
// Generate UUID version 5 - deterministic and hashed with SHA-1
uuid5 := uuid.NewSHA1(namespace, []byte("https://example.com"))
fmt.Printf("UUID v5: %s\n", uuid5.String())
// Generate UUID version 6 - time-ordered (requires additional implementation)
// Note: v6 and v7 may require additional libraries or custom implementation
// Generate UUID version 7 - time-ordered with random
// Note: v7 support may require newer versions or additional libraries
}
Installation & Setup
Go Modules (Recommended):
bash
go mod init your-project
go get github.com/google/uuid
Legacy GOPATH:
bash
go get github.com/google/uuid
Advanced Usage
Parse and Validate UUIDs:
go
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
// Parse UUID from string
uuidStr := "550e8400-e29b-41d4-a716-446655440000"
parsedUUID, err := uuid.Parse(uuidStr)
if err != nil {
fmt.Printf("Invalid UUID: %v\n", err)
return
}
fmt.Printf("Parsed UUID: %s\n", parsedUUID.String())
fmt.Printf("Version: %d\n", parsedUUID.Version())
fmt.Printf("Variant: %d\n", parsedUUID.Variant())
}
Custom Namespace UUIDs:
go
// Create custom namespace
customNamespace := uuid.MustParse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
// Generate deterministic UUIDs
uuid3 := uuid.NewMD5(customNamespace, []byte("my-resource"))
uuid5 := uuid.NewSHA1(customNamespace, []byte("my-resource"))
Microservices & Distributed Systems
Best Practices for Go Services:
go
type Service struct {
generateID func() string
}
func NewService() *Service {
return &Service{
generateID: func() string {
return uuid.New().String()
},
}
}
// For database-friendly IDs, consider time-ordered UUIDs
func (s *Service) CreateResource() string {
return s.generateID()
}
UUID Version Comparison
Choose the right version for your Go application:
- Version 1 - Time-based, includes MAC address
- Version 3 - MD5 namespace-based, deterministic
- Version 4 - Random, most popular choice
- Version 5 - SHA-1 namespace-based, more secure than v3
- Version 6 - Time-ordered, better than v1 for databases
- Version 7 - Modern time-based with improved sorting
For Go applications:
- Microservices: Use Version 4 for request IDs and entities
- Distributed systems: Consider Version 7 for time-ordered events
- Cloud-native apps: Use Version 6 for database optimization
How do I generate UUID in other languages?
Systems programming:
Web and enterprise:
- JavaScript - crypto.randomUUID() & uuid library
- Python - Built-in uuid module
- Java - java.util.UUID & UuidCreator
- C# - System.Guid & UuidCreator
- PHP - Built-in functions & ramsey/uuid