Is there a method to generate a UUID with go language

后端 未结 12 1739
温柔的废话
温柔的废话 2020-12-07 12:15

I have code that looks like this:

u := make([]byte, 16)
_, err := rand.Read(u)
if err != nil {
    return
}

u[8] = (u[8] | 0x80) & 0xBF // what does thi         


        
12条回答
  •  我在风中等你
    2020-12-07 12:38

    As part of the uuid spec, if you generate a uuid from random it must contain a "4" as the 13th character and a "8", "9", "a", or "b" in the 17th (source).

    // this makes sure that the 13th character is "4"
    u[6] = (u[6] | 0x40) & 0x4F
    // this makes sure that the 17th is "8", "9", "a", or "b"
    u[8] = (u[8] | 0x80) & 0xBF 
    

提交回复
热议问题