1 // Copyright 2011 Google Inc. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 guuid "github.com/google/uuid"
16 // Array is a pass-by-value UUID that can be used as an effecient key in a map.
19 // UUID converts uuid into a slice.
20 func (uuid Array) UUID() UUID {
24 // String returns the string representation of uuid,
25 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
26 func (uuid Array) String() string {
27 return guuid.UUID(uuid).String()
30 // A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC
34 // A Version represents a UUIDs version.
35 type Version = guuid.Version
37 // A Variant represents a UUIDs variant.
38 type Variant = guuid.Variant
40 // Constants returned by Variant.
42 Invalid = guuid.Invalid // Invalid UUID
43 RFC4122 = guuid.RFC4122 // The variant specified in RFC4122
44 Reserved = guuid.Reserved // Reserved, NCS backward compatibility.
45 Microsoft = guuid.Microsoft // Reserved, Microsoft Corporation backward compatibility.
46 Future = guuid.Future // Reserved for future definition.
49 var rander = rand.Reader // random function
51 // New returns a new random (version 4) UUID as a string. It is a convenience
52 // function for NewRandom().String().
54 return NewRandom().String()
57 // Parse decodes s into a UUID or returns nil. See github.com/google/uuid for
58 // the formats parsed.
59 func Parse(s string) UUID {
60 gu, err := guuid.Parse(s)
67 // ParseBytes is like Parse, except it parses a byte slice instead of a string.
68 func ParseBytes(b []byte) (UUID, error) {
69 gu, err := guuid.ParseBytes(b)
76 // Equal returns true if uuid1 and uuid2 are equal.
77 func Equal(uuid1, uuid2 UUID) bool {
78 return bytes.Equal(uuid1, uuid2)
81 // Array returns an array representation of uuid that can be used as a map key.
82 // Array panics if uuid is not valid.
83 func (uuid UUID) Array() Array {
92 // String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
93 // , or "" if uuid is invalid.
94 func (uuid UUID) String() string {
99 encodeHex(buf[:], uuid)
100 return string(buf[:])
103 // URN returns the RFC 2141 URN form of uuid,
104 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid.
105 func (uuid UUID) URN() string {
110 copy(buf[:], "urn:uuid:")
111 encodeHex(buf[9:], uuid)
112 return string(buf[:])
115 func encodeHex(dst []byte, uuid UUID) {
116 hex.Encode(dst[:], uuid[:4])
118 hex.Encode(dst[9:13], uuid[4:6])
120 hex.Encode(dst[14:18], uuid[6:8])
122 hex.Encode(dst[19:23], uuid[8:10])
124 hex.Encode(dst[24:], uuid[10:])
127 // Variant returns the variant encoded in uuid. It returns Invalid if
129 func (uuid UUID) Variant() Variant {
134 case (uuid[8] & 0xc0) == 0x80:
136 case (uuid[8] & 0xe0) == 0xc0:
138 case (uuid[8] & 0xe0) == 0xe0:
145 // Version returns the version of uuid. It returns false if uuid is not
147 func (uuid UUID) Version() (Version, bool) {
151 return Version(uuid[6] >> 4), true
154 // SetRand sets the random number generator to r, which implements io.Reader.
155 // If r.Read returns an error when the package requests random data then
156 // a panic will be issued.
158 // Calling SetRand with nil sets the random number generator to the default
160 func SetRand(r io.Reader) {