Skip to main content

How to Generate a UUID in Go with the uuid Package

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Since Go 1.27, the standard library has a uuid package, so uuid.New() makes a random UUID with no module to add. It returns a uuid.UUID, which prints as the familiar 36 characters, and uuid.NewV7() makes a time-ordered one.

Syntax​

Syntax
import "uuid"

<id> := uuid.New()
<text> := <id>.String()
PartWhat it does
import "uuid"Loads the standard library's uuid package. It's part of Go, so there's no go get.
uuid.New()Makes a new UUID. Today it's a random version 4 UUID.
String()Returns the UUID as text in the usual form with dashes.

The package documentation for New: "Programs which do not have a need for a specific UUID generation algorithm should use New. At this time, New is equivalent to NewV4." The Go 1.27 release notes announce it: "The new uuid package generates and parses UUIDs."

The Example​

The example makes a UUID and prints it and the length of its text, then makes a second one, and finally a version 7 UUID with uuid.NewV7().

Prerequisites​

  • Go 1.27 or later. This guide uses Go 1.27.4.

Step 1: Write the Code​

Create a file named main.go:

main.go, which imports fmt and uuid, stores uuid.New() in id, prints it and the length of id.String(), then prints a second uuid.New() and a uuid.NewV7().

main.go
package main

import (
"fmt"
"uuid"
)

func main() {
id := uuid.New()
fmt.Println(id)
fmt.Println(len(id.String()))
fmt.Println(uuid.New())
fmt.Println(uuid.NewV7())
}

Step 2: Run It​

Terminal
go run main.go
Output
b60ec319-b567-4d44-ae46-8b8bcf7112f6
36
d75f7c9b-aa45-4dcc-ac1e-793390182978
01a22d7b-d361-7d06-a4a0-3a4e4c2eac65

A zsh terminal where go run main.go prints a UUID, the length 36, a second random UUID and a version 7 UUID starting with 01a22d7b.

The command is the same in zsh, bash and PowerShell. Your UUIDs will differ.

What Happened​

  • fmt.Println(id) printed text, because uuid.UUID has a String method that fmt calls.
  • The text is 36 characters long: 32 hexadecimal digits and four dashes.
  • The two uuid.New() values are unrelated. Both have a 4 at the start of their third group, the version digit of a random UUID.
  • The NewV7 UUID has a 7 there instead, and it starts with the current time. The documentation says these "contain a timestamp in the most significant 48 bits, and at least 62 bits of random data", so newer ones sort after older ones.

Common Mistakes​

MistakeWhat happensDo this instead
Looking for a uuid module to add with go getThe package is part of the standard library since Go 1.27Just import "uuid"
Using an older GoThe package doesn't exist before Go 1.27Check go version, and install Go 1.27 or later
Storing the UUID where a string is expecteduuid.UUID is an array of 16 bytes, not textCall .String()

Troubleshooting​

SymptomLikely causeFix
The uuid import isn't foundThe Go toolchain is older than 1.27, which added the packageCheck go version, and upgrade Go
A UUID with a 7 as its version digitIt came from NewV7, not NewUse New for a random UUID

FAQ​

Is uuid.New random enough for IDs that must not be guessed?​

Yes. The package documentation says "Random components of new UUIDs are generated with a cryptographically secure random number generator."

How do I turn text back into a UUID?​

uuid.Parse(text) returns the UUID and an error, which is set when the text isn't a UUID.

Conclusion​

uuid.New() from Go's standard library makes a random UUID, and .String() gives you its text. Reach for uuid.NewV7() when you want IDs that sort by time.

Additional Resources​