github.com/hernad/nomad@v1.6.112/helper/uuid/uuid.go (about)

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