How to Generate a UUID in Go with the uuid Package
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
import "uuid"
<id> := uuid.New()
<text> := <id>.String()
| Part | What 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:

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
go run main.go
b60ec319-b567-4d44-ae46-8b8bcf7112f6
36
d75f7c9b-aa45-4dcc-ac1e-793390182978
01a22d7b-d361-7d06-a4a0-3a4e4c2eac65

The command is the same in zsh, bash and PowerShell. Your UUIDs will differ.
What Happened
fmt.Println(id)printed text, becauseuuid.UUIDhas aStringmethod thatfmtcalls.- The text is 36 characters long: 32 hexadecimal digits and four dashes.
- The two
uuid.New()values are unrelated. Both have a4at the start of their third group, the version digit of a random UUID. - The
NewV7UUID has a7there 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
| Mistake | What happens | Do this instead |
|---|---|---|
Looking for a uuid module to add with go get | The package is part of the standard library since Go 1.27 | Just import "uuid" |
| Using an older Go | The package doesn't exist before Go 1.27 | Check go version, and install Go 1.27 or later |
Storing the UUID where a string is expected | uuid.UUID is an array of 16 bytes, not text | Call .String() |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The uuid import isn't found | The Go toolchain is older than 1.27, which added the package | Check go version, and upgrade Go |
A UUID with a 7 as its version digit | It came from NewV7, not New | Use 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
- uuid, in the Go standard library documentation.
- Go 1.27 release notes.
