google / uuid

Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.
BSD 3-Clause "New" or "Revised" License
5.26k stars 362 forks source link

Casting byte slice to UUID? #21

Closed phbcanada closed 6 years ago

phbcanada commented 6 years ago

We are passing 16 byte binary UUIDs via grpc and need to be able to cast these from byte slice back to type uuid.UUID. There doesn't seem to be an easy way to do this. It would be nice if the package had a method for this. This works but is there a better way?

   var b [16]byte

    copy(b[:], bslice)
    guid := uuid.UUID(b)
loderunner commented 6 years ago

We had the exact same issue with converting grpc bytes to uuid.UUID so I built a convenience constructor. It was merged with this pull request.

Here's how to create a uuid.UUID from a byte slice.

b := []byte{
    0x7d, 0x44, 0x48, 0x40,
    0x9d, 0xc0,
    0x11, 0xd1,
    0xb2, 0x45,
    0x5f, 0xfd, 0xce, 0x74, 0xfa, 0xd2,
}
buuid, err := uuid.FromBytes(b)
phbcanada commented 6 years ago

Awesome. Thanks!