add uuid functions
This commit is contained in:
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
||||
module git.sunturtle.xyz/zephyr/rand
|
||||
|
||||
go 1.24.1
|
||||
|
||||
require github.com/google/uuid v1.6.0
|
||||
|
||||
2
go.sum
Normal file
2
go.sum
Normal file
@@ -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=
|
||||
17
uuid.go
Normal file
17
uuid.go
Normal file
@@ -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()
|
||||
}
|
||||
29
uuid_test.go
Normal file
29
uuid_test.go
Normal file
@@ -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())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user