github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/uuid/uuid.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package uuid
     4  
     5  import (
     6  	"crypto/rand"
     7  	"io"
     8  )
     9  
    10  /*
    11  UUID representation compliant with specification described in https://tools.ietf.org/html/rfc4122.
    12  
    13  Use V1 for massive data that don't need to read much specially very close to write time!
    14  These type of records write sequently in cluster and don't need very much move in cluster expand proccess!
    15  */
    16  
    17  // NewV1 generate version 1 UUID include date-time and MAC address
    18  func NewV1() (uuid [16]byte) {
    19  	return
    20  }
    21  
    22  // NewV4 generate version 4 UUID include randomly numbers.
    23  func NewV4() (uuid [16]byte) {
    24  	var err error
    25  	_, err = io.ReadFull(rand.Reader, uuid[:])
    26  	if err != nil {
    27  		// TODO::: make random by other ways
    28  	}
    29  
    30  	// Set version to 4
    31  	uuid[6] = (uuid[6] & 0x0f) | (0x04 << 4)
    32  	// Set variant to RFC4122
    33  	uuid[8] = (uuid[8]&(0xff>>2) | (0x02 << 6))
    34  
    35  	return
    36  }
    37  
    38  const (
    39  	personUserID = 1 + iota
    40  	orgUserID
    41  )
    42  
    43  // SetUserType use to set user type in any UUID generated!
    44  func SetUserType(uuid [16]byte, userType uint8) [16]byte {
    45  	//  first 4 bit use!
    46  	return uuid
    47  }