Is there a method to generate a UUID with go language

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

    The go-uuid library is NOT RFC4122 compliant. The variant bits are not set correctly. There have been several attempts by community members to have this fixed but pull requests for the fix are not being accepted.

    You can generate UUIDs using the Go uuid library I rewrote based on the go-uuid library. There are several fixes and improvements. This can be installed with:

    go get github.com/twinj/uuid
    

    You can generate random (version 4) UUIDs with:

    import "github.com/twinj/uuid"
    
    u := uuid.NewV4()
    

    The returned UUID type is an interface and the underlying type is an array.

    The library also generates v1 UUIDs and correctly generates v3 and 5 UUIDs. There are several new methods to help with printing and formatting and also new general methods to create UUIDs based off of existing data.

提交回复
热议问题