Source File
uuid.go
Belonging Package
github.com/aws/smithy-go/rand
package rand
import (
)
const dash byte = '-'
// UUIDIdempotencyToken provides a utility to get idempotency tokens in the
// UUID format.
type UUIDIdempotencyToken struct {
uuid *UUID
}
// NewUUIDIdempotencyToken returns a idempotency token provider returning
// tokens in the UUID random format using the reader provided.
func ( io.Reader) *UUIDIdempotencyToken {
return &UUIDIdempotencyToken{uuid: NewUUID()}
}
// GetIdempotencyToken returns a random UUID value for Idempotency token.
func ( UUIDIdempotencyToken) () (string, error) {
return .uuid.GetUUID()
}
// UUID provides computing random UUID version 4 values from a random source
// reader.
type UUID struct {
randSrc io.Reader
}
// NewUUID returns an initialized UUID value that can be used to retrieve
// random UUID version 4 values.
func ( io.Reader) *UUID {
return &UUID{randSrc: }
}
// GetUUID returns a random UUID version 4 string representation sourced from the random reader the
// UUID was created with. Returns an error if unable to compute the UUID.
func ( *UUID) () (string, error) {
var [16]byte
if , := io.ReadFull(.randSrc, [:]); != nil {
return "",
}
.makeUUIDv4([:])
return format(), nil
}
// GetBytes returns a byte slice containing a random UUID version 4 sourced from the random reader the
// UUID was created with. Returns an error if unable to compute the UUID.
func ( *UUID) () ( []byte, error) {
= make([]byte, 16)
if _, = io.ReadFull(.randSrc, ); != nil {
return ,
}
.makeUUIDv4()
return , nil
}
func ( *UUID) ( []byte) {
// 13th character is "4"
[6] = ([6] & 0x0f) | 0x40 // Version 4
// 17th character is "8", "9", "a", or "b"
[8] = ([8] & 0x3f) | 0x80 // Variant most significant bits are 10x where x can be either 1 or 0
}
// Format returns the canonical text representation of a UUID.
// This implementation is optimized to not use fmt.
// Example: 82e42f16-b6cc-4d5b-95f5-d403c4befd3d
func ( [16]byte) string {
// https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29
var [36]byte
hex.Encode([:8], [0:4])
[8] = dash
hex.Encode([9:13], [4:6])
[13] = dash
hex.Encode([14:18], [6:8])
[18] = dash
hex.Encode([19:23], [8:10])
[23] = dash
hex.Encode([24:], [10:])
return string([:])
}
The pages are generated with Golds v0.4.9. (GOOS=linux GOARCH=amd64)