-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
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() stringAs 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.
- baseline (all uuid types) 487k: https://github.com/search?q=language%3AGo%20%2Fuuid%5C.New%2F&type=code
- generating v4 (uuid.New, uuid.NewString, uuid.NewRandom, uuid.NewUUID) 401k: https://github.com/search?q=language%3AGo+%2Fuuid%5C.New%28String%7CRandom%7CUUID%29%3F%5C%28%2F&type=code
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
- generating v7 (uuid.NewV7) 5.6k: https://github.com/search?q=language%3AGo+%2Fuuid%5C.NewV7%5C%28%2F&type=code
- all other uuids 2.4k: https://github.com/search?q=language%3AGo+%2Fuuid%5C.New%28V6%7CMD5%7CSHA1%7CHash%7CDCE.*%29%5C%28%2F&type=code
cc @FiloSottile
Metadata
Metadata
Assignees
Labels
Type
Projects
Status