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

add FromBytes constructor #23

Closed loderunner closed 6 years ago

loderunner commented 6 years ago

FromBytes is a convenience constructor. It relies on UnmarshalBinary to create a new UUID. This makes creating a new UUID from a slice a bytes clearer in the client code without having to create a temporary variable, and more consistent with creating a slice by parsing a string.

Example:

s := "f47ac10b-58cc-0372-8567-0e02b2c3d479"
suuid, err := uuid.Parse(s)
b := []byte{
    0x7d, 0x44, 0x48, 0x40,
    0x9d, 0xc0,
    0x11, 0xd1,
    0xb2, 0x45,
    0x5f, 0xfd, 0xce, 0x74, 0xfa, 0xd2,
}
buuid, err := uuid.FromBytes(b)

I realize FromBytes is a bit of a name collision with ParseBytes. I am open to better suggestions for naming, of course. In fact, when discovering the library, I originally expected ParseBytes to perform this copy.

googlebot commented 6 years ago

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


loderunner commented 6 years ago

I signed it!

googlebot commented 6 years ago

CLAs look good, thanks!