github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/helper/uuid/uuid.go (about)

     1  package uuid
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/nomad/helper/crypto"
     7  )
     8  
     9  // Generate is used to generate a random UUID.
    10  func Generate() string {
    11  	buf, err := crypto.Bytes(16)
    12  	if err != nil {
    13  		panic(fmt.Errorf("failed to read random bytes: %v", err))
    14  	}
    15  
    16  	return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
    17  		buf[0:4],
    18  		buf[4:6],
    19  		buf[6:8],
    20  		buf[8:10],
    21  		buf[10:16])
    22  }
    23  
    24  // Short is used to generate the first 8 characters of a UUID.
    25  func Short() string {
    26  	return Generate()[0:8]
    27  }