如何在golang中生成确定性的UUID集合

huangapple go评论67阅读模式
英文:

How to generate a deterministic set of UUIDs in golang

问题

我正在进行一些测试,有一个已知的UUID集合在我们的代码中使用会很有用。然而,我在尝试在golang中创建一组确定性UUID时遇到了问题。

我尝试了几种方法,但都没有成功:


type KnownReader struct {
	store *Store
}

type Store struct {
	val uint16
}

func (r KnownReader) Read(p []byte) (n int, err error) {
    ret := r.store.val
	r.store.val = ret + 1
	fmt.Printf("\nStore: %v", r.store.val)
    p = make([]byte, 4)
	binary.LittleEndian.PutUint16(p, uint16(ret))
    fmt.Printf("\nreader p: % x", p)
    return binary.MaxVarintLen16, nil
}

func main() {
    r := KnownReader{
		store: &Store{val: 111},
	}
    uuid.SetRand(r)
	u, _ := uuid.NewRandomFromReader(r)
	fmt.Printf("\n%v",u)
    u, _ = uuid.NewRandomFromReader(r)
	fmt.Printf("\n%v",u)
}

---- 输出 ----

Store: 1
reader p: 00 00 00 00
Store: 2
reader p: 01 00 00 00
Store: 3
reader p: 02 00 00 00
Store: 4
reader p: 03 00 00 00
Store: 5
reader p: 04 00 00 00
Store: 6
reader p: 05 00 00 00
00000000-0000-4000-8000-000000000000
Store: 7
reader p: 06 00 00 00
Store: 8
reader p: 07 00 00 00
Store: 9
reader p: 08 00 00 00
Store: 10
reader p: 09 00 00 00
Store: 11
reader p: 0a 00 00 00
Store: 12
reader p: 0b 00 00 00
00000000-0000-4000-8000-000000000000

如你所见,UUID在调用之间没有改变。

我还尝试使用uuid.FromBytes,但似乎也不起作用:


func getbytes(num uint16) []byte {
	p := make([]byte, 4)
	binary.LittleEndian.PutUint16(p, num)
	fmt.Printf("\ngetbytes p: % x", p)
	return p
}

func main() {
    var i uint16 = 0
	fmt.Printf("\nout getbytes: % x", getbytes(i))
	u, _ := uuid.FromBytes(getbytes(i))
	i = i + 1
	fmt.Printf("\nUUID: %v", u)
	fmt.Printf("\nout getbytes: % x", getbytes(i))
	u, _ = uuid.FromBytes(getbytes(i))
	fmt.Printf("\nUUID: %v", u)
}

---- 输出 ----

getbytes p: 00 00 00 00
out getbytes: 00 00 00 00
getbytes p: 00 00 00 00
UUID: 00000000-0000-0000-0000-000000000000
getbytes p: 01 00 00 00
out getbytes: 01 00 00 00
getbytes p: 01 00 00 00
UUID: 00000000-0000-0000-0000-000000000000

如你所见,UUID在这里仍然是相同的。

那么,我是否遗漏了什么?我如何获得一组一致的UUID?

谢谢。

英文:

I'm doing some testing and it would be useful to have a known set of UUIDs that are getting used by our code. However, I'm having trouble figuring out how to create a deterministic set of UUIDs in golang.

I've tried a few approaches, but neither seemed to work:


type KnownReader struct {
	store *Store
}

type Store struct {
	val uint16
}

func (r KnownReader) Read(p []byte) (n int, err error) {
    ret := r.store.val
	r.store.val = ret + 1
	fmt.Printf("\nStore: %v", r.store.val)
    p = make([]byte, 4)
	binary.LittleEndian.PutUint16(p, uint16(ret))
    fmt.Printf("\nreader p: % x", p)
    return binary.MaxVarintLen16, nil
}

func main() {
    r := KnownReader{
		store: &Store{val: 111},
	}
    uuid.SetRand(r)
	u, _ := uuid.NewRandomFromReader(r)
	fmt.Printf("\n%v",u)
    u, _ = uuid.NewRandomFromReader(r)
	fmt.Printf("\n%v",u)
}

---- OUTPUT ----

Store: 1
reader p: 00 00 00 00
Store: 2
reader p: 01 00 00 00
Store: 3
reader p: 02 00 00 00
Store: 4
reader p: 03 00 00 00
Store: 5
reader p: 04 00 00 00
Store: 6
reader p: 05 00 00 00
00000000-0000-4000-8000-000000000000
Store: 7
reader p: 06 00 00 00
Store: 8
reader p: 07 00 00 00
Store: 9
reader p: 08 00 00 00
Store: 10
reader p: 09 00 00 00
Store: 11
reader p: 0a 00 00 00
Store: 12
reader p: 0b 00 00 00
00000000-0000-4000-8000-000000000000

As you can see, the UUID, does not change between calls

I also tried using uuid.FromBytes, but that didn't seem to work either:


func getbytes(num uint16) []byte {
	p := make([]byte, 4)
	binary.LittleEndian.PutUint16(p, num)
	fmt.Printf("\ngetbytes p: % x", p)
	return p
}

func main() {
    var i uint16 = 0
	fmt.Printf("\nout getbytes: % x", getbytes(i))
	u, _ := uuid.FromBytes(getbytes(i))
	i = i + 1
	fmt.Printf("\nUUID: %v", u)
	fmt.Printf("\nout getbytes: % x", getbytes(i))
	u, _ = uuid.FromBytes(getbytes(i))
	fmt.Printf("\nUUID: %v", u)
}

---- OUTPUT ----

getbytes p: 00 00 00 00
out getbytes: 00 00 00 00
getbytes p: 00 00 00 00
UUID: 00000000-0000-0000-0000-000000000000
getbytes p: 01 00 00 00
out getbytes: 01 00 00 00
getbytes p: 01 00 00 00
UUID: 00000000-0000-0000-0000-000000000000

As you can see the UUIDs are still the same here as well.

So, is there something I'm missing? How can I get a consistent set of UUIDs?

Thanks

答案1

得分: 2

感谢Adrian,我想我找到了答案:

rnd := rand.New(rand.NewSource(1))
uuid.SetRand(rnd)
u, _ = uuid.NewRandomFromReader(rnd)
fmt.Printf("\n%v", u)
u, _ = uuid.NewRandomFromReader(rnd)
fmt.Printf("\n%v", u)

--- 输出 ---

52fdfc07-2182-454f-963f-5f0f9a621d72
9566c74d-1003-4c4d-bbbb-0407d1e2c649

英文:

Thanks Adrian, I think I figured out the answer:

rnd := rand.New(rand.NewSource(1))
uuid.SetRand(rnd)
u, _ = uuid.NewRandomFromReader(rnd)
fmt.Printf("\n%v", u)
u, _ = uuid.NewRandomFromReader(rnd)
fmt.Printf("\n%v", u)

--- OUTPUT ---

52fdfc07-2182-454f-963f-5f0f9a621d72
9566c74d-1003-4c4d-bbbb-0407d1e2c649

huangapple
  • 本文由 发表于 2021年5月28日 04:51:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/67729822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定