// go get github.com/google/uuid
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
// v4 — random (most common)
id := uuid.New()
fmt.Println(id) // f47ac10b-58cc-4372-a567-0e02b2c3d479
// v7 — time-ordered (Go 1.6+ of the library)
id7, _ := uuid.NewV7()
fmt.Println(id7) // 01928f4d-... (chronologically sortable)
// Parse
parsed, err := uuid.Parse("f47ac10b-58cc-4372-a567-0e02b2c3d479")
if err != nil { panic(err) }
fmt.Println(parsed.Version(), parsed.Variant())
// Bytes / String round-trip
b, _ := id.MarshalBinary() // 16 raw bytes
parsed2, _ := uuid.FromBytes(b)
fmt.Println(parsed2 == id) // true
}
Create a free account and build your private vault. Share publicly whenever you want.