Generate a unique string (UUID, GUID)

yourbasic.org/golang

A universally unique identifier (UUID), or globally unique identifier (GUID), is a 128-bit number used to identify information.

The string representation of a UUID consists of 32 hexadecimal digits displayed in 5 groups separated by hyphens. For example:

123e4567-e89b-12d3-a456-426655440000

UUID generator example

You can use the rand.Read function from package crypto/rand to generate a basic UUID.

b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
	log.Fatal(err)
}
uuid := fmt.Sprintf("%x-%x-%x-%x-%x",
	b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
fmt.Println(uuid)
9438167c-9493-4993-fd48-950b27aad7c9

Limitations

This UUID doesn’t conform to RFC 4122. In particular, it doesn’t contain any version or variant numbers.

Warning: The rand.Read call returns an error if the underlying system call fails. For instance if it can't read /dev/urandom on a Unix system, or if CryptAcquireContext fails on a Windows system.

Further reading

Generate random numbers, characters and slice elements

Share this page: