Is there a method to generate a UUID with go language

后端 未结 12 1713
温柔的废话
温柔的废话 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:28

    On Linux, you can read from /proc/sys/kernel/random/uuid:

    package main
    
    import "io/ioutil"
    import "fmt"
    
    func main() {
        u, _ := ioutil.ReadFile("/proc/sys/kernel/random/uuid")
        fmt.Println(string(u))
    }
    

    No external dependencies!

    $ go run uuid.go 
    3ee995e3-0c96-4e30-ac1e-f7f04fd03e44
    

提交回复
热议问题