In Go, you can use the uuid package to generate universally unique identifiers (UUIDs). Here’s an example of how to generate a random UUID:
package main
import (
"fmt"
uuid "github.com/satori/go.uuid"
)
func main() {
// Generate a random UUID
u1 := uuid.NewV4()
fmt.Printf("UUIDv4: %s\n", u1)
}
This will output a string representation of a UUID in the form of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
, where x is a hexadecimal digit.
If you need to generate a UUID based on a specific input, you can use the NewV5 function and provide a namespace and a name as arguments. For example:
u2 := uuid.NewV5(uuid.NamespaceURL, []byte("example.com"))
fmt.Printf("UUIDv5: %s\n", u2)
This will generate a UUID based on the namespace uuid.NamespaceURL
and the name "example.com"
.