Generate a unique string (UUID, GUID)

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:


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 {
uuid := fmt.Sprintf("%x-%x-%x-%x-%x",
    b[0:4], b[4:6], b[6:8], b[8:10], b[10:])


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: