github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/helper/uuid/uuid.go (about)

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