diff options
| author | Shulhan <ms@kilabit.info> | 2026-04-06 16:29:02 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-04-06 16:29:02 +0700 |
| commit | 0fd9dc3b4b9fe7acc0ebc498c2ab05a2e95a1239 (patch) | |
| tree | 7072a3ef2035531e1ee1cb888fb602ed32251e45 /lib | |
| parent | 6049c2e86450464d6bfdbbdb4fa2b4d64912ca01 (diff) | |
| download | pakakeh.go-0fd9dc3b4b9fe7acc0ebc498c2ab05a2e95a1239.tar.xz | |
lib/uuidv7: add suffix Func to Now and Rand
Using Func suffix to function variable is common coding styles in Go.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/uuidv7/example_test.go | 14 | ||||
| -rw-r--r-- | lib/uuidv7/uuid.go | 14 |
2 files changed, 15 insertions, 13 deletions
diff --git a/lib/uuidv7/example_test.go b/lib/uuidv7/example_test.go index 86ae7aab..2e23e6d1 100644 --- a/lib/uuidv7/example_test.go +++ b/lib/uuidv7/example_test.go @@ -15,13 +15,13 @@ import ( ) func ExampleGenerate() { - // Begin mocking Now and Rand, DO NOT USE in production code. + // Begin mocking NowFunc and RandFunc, DO NOT USE in production code. now := time.Date(2026, 3, 9, 14, 20, 0, 123456700, time.UTC) - uuidv7.Now = func() time.Time { + uuidv7.NowFunc = func() time.Time { now = now.Add(time.Second) return now } - uuidv7.Rand = func() uint64 { + uuidv7.RandFunc = func() uint64 { return 0xFFFF_FFFF_FFFF_FFFF } @@ -80,11 +80,11 @@ func ExampleUUID_IsEqual() { func ExampleUUID_MarshalBinary() { now := time.Date(2026, 3, 9, 14, 20, 0, 123456700, time.UTC) - uuidv7.Now = func() time.Time { + uuidv7.NowFunc = func() time.Time { now = now.Add(time.Second) return now } - uuidv7.Rand = func() uint64 { + uuidv7.RandFunc = func() uint64 { return 0xFFFF_FFFF_FFFF_FFFF } @@ -112,11 +112,11 @@ func ExampleUUID_MarshalBinary() { func ExampleUUID_MarshalText() { now := time.Date(2026, 3, 9, 14, 20, 0, 123456700, time.UTC) - uuidv7.Now = func() time.Time { + uuidv7.NowFunc = func() time.Time { now = now.Add(time.Second) return now } - uuidv7.Rand = func() uint64 { + uuidv7.RandFunc = func() uint64 { return 0xFFFF_FFFF_FFFF_FFFF } diff --git a/lib/uuidv7/uuid.go b/lib/uuidv7/uuid.go index 203b41cf..43e293ca 100644 --- a/lib/uuidv7/uuid.go +++ b/lib/uuidv7/uuid.go @@ -16,15 +16,17 @@ import ( const hexTable = `0123456789ABCDEF` const variantMask uint64 = 0x8000_0000_0000_0000 -// Now defines the function variable that return the current time in UTC. +// NowFunc defines the function variable that return the current time in UTC. // This identifier is exported to simplify testing. -var Now = func() time.Time { +var NowFunc = func() time.Time { return time.Now().UTC() } -// Rand defines the function variable that return a random uint64. +// RandFunc defines the function variable that return a random uint64. // This identifier is exported to simplify testing. -var Rand = func() (v uint64) { +// User with Go 1.26 or later should use [testing/cryptotest.SetGlobalRandom] +// function to mock the random generator. +var RandFunc = func() (v uint64) { b := make([]byte, 8) rand.Read(b) v, _ = binary.Uvarint(b) @@ -45,7 +47,7 @@ type UUID struct { // Generate generates new UUID version 7. func Generate() (id UUID) { - now := Now() + now := NowFunc() generate(&now, &id) return id } @@ -58,7 +60,7 @@ func generate(t *time.Time, id *UUID) { prec := unixns - (millis * 1e6) // 456700 clockPrec := uint64((prec * 4096) / 1e6) // 1870 id.high |= clockPrec // 0x019c_d2f8_1ecb_774e - rand := Rand() // 0xffff_ffff_ffff_ffff + rand := RandFunc() // 0xffff_ffff_ffff_ffff rand >>= 2 // 0x3fff_ffff_ffff_ffff id.low = variantMask | rand // 0xbfff_ffff_ffff_ffff } |
