By Stefan Nilsson
The original url: yourbasic.org/golang/gene…
Universally Unique Identifiers (UUID) or globally unique Identifiers (GUID) are 128-bit numbers used to identify information.
- The UUID is unique (for practical purposes) : its probability of repetition is very close to zero.
- UUID does not depend on registration and assignment by a central agency.
The string representation of a UUID consists of 32 hexadecimal digits, which are displayed in five groups separated by hyphens (-). Such as:
123e4567-e89b-12d3-a456-42665544 0000Copy the code
Example UUID generator
You can use the rand.read function from the Crypto/RAND package to generate the basic UUID.
b := make([]byte.16)
_, err := rand.Read(b)
iferr ! =nil {
log.Fatal(err)
}
uuid := fmt.Sprintf("%x-%x-%x-%x-%x",
b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
fmt.Println(uuid)
Copy the code
Output:
9438167c- 9493.- 4993.-fd48- 950.b27aad7c9
Copy the code
limitations
This UUID does not comply with RFC4122. In particular, it does not contain any version or variant numbers.
Warning: If the underlying system call fails, the rand.read call returns an error. For example, if it fails to read /dev/urandom on a Unix system, or the CryptAcquireContext call fails on a Windows system.
read
Generate random numbers, characters and slice elements in Golang
Scan the QR code below to follow the Feed and regularly push the latest posts