Skip to content

proposal: crypto/rand: add UUIDv4 and UUIDv7 #76319

@seankhliao

Description

@seankhliao

Proposal Details

// UUIDv4 generates a RFC 9562 Version 4 UUID
// using only random bytes in the format: 
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
func UUIDv4() string

// UUIDv7 generates a RFC 9562 Version 7 UUID
// using the current time and random bytes in the format:
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
func UUIDv7() string

As an alternative to #62026,
we propose the addition of 2 new helpers in crypto/rand
similar to the Text helper added in #67057.

We scope the standard library's support to only include generation of UUIDs,
and treat them as generally opaque identifiers.
The functions return a string, sidestepping any questions about marshaling.
The important comparisons are naturally available: ==, !=, strings.Compare (for sorting).
Users needing advanced functionality such as parsing, alternative marshaling, or otherwise inspecting UUIDs
can continue to use github.com/google/uuid (104k imports).

Why v4?

UUDv4 is purely random and should result in an even distribution of IDs.
It is also the most common type in use today.

Why v7?

While less popular by code metrics as v4,
it is still over twice as popular as all other type combined.
The time / sortability of these allow for more efficient use with some database implementations,
as seen by postgres implementing only v4 and v7: https://www.postgresql.org/docs/current/functions-uuid.html

cc @FiloSottile

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposalProposal-CryptoProposal related to crypto packages or other security issues

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions