github.com/altipla-consulting/ravendb-go-client@v0.1.3/uuid.go (about) 1 package ravendb 2 3 import ( 4 "crypto/rand" 5 "encoding/hex" 6 ) 7 8 // implements generating random uuid4 that mimics python's uuid.uuid4() 9 // it doesn't try to fully UUIDv4 compliant 10 11 // UUID represents a random 16-byte number 12 type UUID struct { 13 data [16]byte 14 } 15 16 // NewUUID creates a new UUID 17 func NewUUID() *UUID { 18 res := &UUID{} 19 n, _ := rand.Read(res.data[:]) 20 panicIf(n != 16, "rand.Read() returned %d, expected 16", n) 21 return res 22 } 23 24 // String returns xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx representation 25 func (u *UUID) String() string { 26 buf := make([]byte, 36) 27 28 hex.Encode(buf[0:8], u.data[0:4]) 29 buf[8] = '-' 30 hex.Encode(buf[9:13], u.data[4:6]) 31 buf[13] = '-' 32 hex.Encode(buf[14:18], u.data[6:8]) 33 buf[18] = '-' 34 hex.Encode(buf[19:23], u.data[8:10]) 35 buf[23] = '-' 36 hex.Encode(buf[24:], u.data[10:]) 37 38 return string(buf) 39 } 40 41 // Hex returns hex-encoded version. 42 // Equivalent of python's uuid.uuid4().hex 43 func (u *UUID) Hex() string { 44 dst := make([]byte, 32) 45 n := hex.Encode(dst, u.data[:]) 46 panicIf(n != 32, "hex.Encode() returned %d, expected 32", n) 47 return string(dst) 48 }