Go's standard library has no UUID package, which is why most projects reach for github.com/google/uuid — the de facto standard for generating a 128-bit unique identifier in any Go application. Run go get github.com/google/uuid, then call uuid.New() to produce a random, RFC 9562-compliant v4 UUID in one line, with no external service or configuration required. Below you'll find copy-paste ready code for generating a Go UUID, parsing a string back into a uuid.UUID, and the newer v7 option for time-sortable database keys.
How to Generate a UUID in Go
Install github.com/google/uuid, the community-standard package for UUID generation in Go, then call uuid.New() to get a random v4 identifier — it's the fastest way to add unique IDs to structs, database rows, or API responses. After reading a hyphenated string from input, a python uuid walkthrough shows how to turn it back into an object and handle a malformed value with try/except.
go get github.com/google/uuid
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
id := uuid.New()
fmt.Println(id.String())
// Output: 550e8400-e29b-41d4-a716-446655440000
// Or handle the error explicitly
id2, err := uuid.NewRandom()
if err != nil {
panic(err)
}
fmt.Println(id2)
}
Explanation
go get github.com/google/uuidadds the package to your module — Go's standard library has no built-in UUID generator.uuid.New()generates a random version 4 UUID and is safe for virtually every use case, since it only panics if the system's random source itself fails.uuid.NewRandom()does the same thing but returns an expliciterrorinstead of panicking, letting you handle a failed random read gracefully in code that can't risk a crash.id.String()and Go'sfmt.Printlnboth call theUUIDtype'sStringerimplementation, producing the standard 36-character hyphenated form.
Convert a String to a UUID in Go
When a UUID arrives as a string — from a URL path, a JSON body, or a database column — parse it back into a type-safe uuid.UUID with uuid.Parse() so you can compare, store, or validate it like any other Go value. Because the result is a string rather than an object, a javascript uuid drops straight into JSON, URLs, and template literals.
package main
import (
"fmt"
"log"
"github.com/google/uuid"
)
func main() {
id, err := uuid.Parse("550e8400-e29b-41d4-a716-446655440000")
if err != nil {
log.Fatal("invalid UUID:", err)
}
fmt.Println(id)
// MustParse panics on invalid input - use only for
// hardcoded constants or tests, never on user input
fixed := uuid.MustParse("550e8400-e29b-41d4-a716-446655440000")
fmt.Println(fixed == id)
}
Explanation
uuid.Parse()returns a(uuid.UUID, error)pair, so a malformed string never crashes your program — you always get the chance to handle the error.- The parser accepts the standard hyphenated form as well as unhyphenated hex, URN format, and Microsoft's bracketed GUID style.
uuid.MustParse()skips the error check and panics immediately on invalid input, which is convenient for constants and test fixtures but unsafe for anything coming from a request or config file.uuid.UUIDvalues compare directly with==because the type is a plain 16-byte array under the hood.
Other Options for Generating UUIDs in Go
uuid.New() covers most cases, but github.com/google/uuid v1.6+ also ships uuid.NewV7(), which generates an RFC 9562-compliant time-sortable UUID — a better fit for database primary keys than a fully random v4.
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
// Unix-timestamp-prefixed, sortable, index-friendly
id, err := uuid.NewV7()
if err != nil {
panic(err)
}
fmt.Println(id)
}
Explanation
uuid.NewV7()was added in google/uuid v1.6, so rungo get -u github.com/google/uuidif yourgo.modpins an older version.- A v7 UUID encodes the current Unix millisecond timestamp in its leading bits, so IDs generated later always sort after earlier ones — unlike v4, which is fully random.
- Sortable IDs keep database B-tree indexes more sequential than random v4 keys, reducing page splits and improving insert performance at scale.
- v7 still includes enough random data to stay collision-resistant, so it's safe to use as a public-facing primary key.