What is the method of generating UUID in Go?

In Go language, you can use the github.com/google/uuid package to generate Universally Unique Identifiers (UUIDs). Here is an example code that uses this package to generate UUIDs:

package main

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

func main() {
	// 生成一个随机的UUID
	randomUUID := uuid.New()
	fmt.Println("Random UUID:", randomUUID.String())

	// 根据命名空间和名称生成一个命名空间UUID
	namespaceUUID := uuid.NewSHA1(uuid.NamespaceURL, []byte("example.com"))
	fmt.Println("Namespace UUID:", namespaceUUID.String())
}

Running the above code will output results similar to the following:

Random UUID: 3ebc5a0f-285e-4d8c-87d5-ea6bd47e8f11
Namespace UUID: 5df41881-3aed-3515-88a7-2f4a814cf09e

The function uuid.New() is used to generate a random UUID, while the function uuid.NewSHA1() can generate a namespace UUID based on a namespace and name.

bannerAds