1
// Package fastuuid provides fast UUID generation of 192 bit
2
// universally unique identifiers. It does not provide
3
// formatting or parsing of the identifiers (it is assumed
4
// that a simple hexadecimal or base64 representation
5
// is sufficient, for which adequate functionality exists elsewhere).
7
// Note that the generated UUIDs are not unguessable - each
8
// UUID generated from a Generator is adjacent to the
9
// previously generated UUID.
11
// It ignores RFC 4122.
21
// Generator represents a UUID generator that
22
// generates UUIDs in sequence from a random starting
24
type Generator struct {
29
// NewGenerator returns a new Generator.
30
// It can fail if the crypto/rand read fails.
31
func NewGenerator() (*Generator, error) {
33
_, err := rand.Read(g.seed[:])
35
return nil, errors.New("cannot generate random seed: " + err.Error())
40
// MustNewGenerator is like NewGenerator
41
// but panics on failure.
42
func MustNewGenerator() *Generator {
43
g, err := NewGenerator()
50
// Next returns the next UUID from the generator.
51
// Only the first 8 bytes can differ from the previous
52
// UUID, so taking a slice of the first 16 bytes
53
// is sufficient to provide a somewhat less secure 128 bit UUID.
55
// It is OK to call this method concurrently.
56
func (g *Generator) Next() [24]byte {
57
x := atomic.AddUint64(&g.counter, 1)
58
var counterBytes [8]byte
59
binary.LittleEndian.PutUint64(counterBytes[:], x)
62
for i, b := range counterBytes {