From 8b2ca71f6b54a2d785732c4f2b2eeb795ba6fb76 Mon Sep 17 00:00:00 2001 From: Branden J Brown Date: Fri, 21 Nov 2025 10:29:40 -0500 Subject: [PATCH] add uuid functions --- go.mod | 2 ++ go.sum | 2 ++ uuid.go | 17 +++++++++++++++++ uuid_test.go | 29 +++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 go.sum create mode 100644 uuid.go create mode 100644 uuid_test.go diff --git a/go.mod b/go.mod index 48f07e2..db1d552 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.sunturtle.xyz/zephyr/rand go 1.24.1 + +require github.com/google/uuid v1.6.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7790d7c --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/uuid.go b/uuid.go new file mode 100644 index 0000000..5942c15 --- /dev/null +++ b/uuid.go @@ -0,0 +1,17 @@ +package rand + +import "github.com/google/uuid" + +// UUIDv4 generates a RFC 9562 Version 4 UUID +// using only random bytes in the format: +// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. +func UUIDv4() string { + return uuid.New().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 { + return uuid.Must(uuid.NewV7()).String() +} diff --git a/uuid_test.go b/uuid_test.go new file mode 100644 index 0000000..7c4f2d7 --- /dev/null +++ b/uuid_test.go @@ -0,0 +1,29 @@ +package rand_test + +import ( + "testing" + + "github.com/google/uuid" + + "git.sunturtle.xyz/zephyr/rand" +) + +func TestUUIDv4(t *testing.T) { + u, err := uuid.Parse(rand.UUIDv4()) + if err != nil { + t.Error(err) + } + if u.Version() != 4 { + t.Errorf("wrong version: want 4, got %d", u.Version()) + } +} + +func TestUUIDv7(t *testing.T) { + u, err := uuid.Parse(rand.UUIDv7()) + if err != nil { + t.Error(err) + } + if u.Version() != 7 { + t.Errorf("wrong version: want 7, got %d", u.Version()) + } +}