Friday, 10 May 2019

Go language: Generate random uuid or string


Below snippet generate random string.

func GenerateRandomString() *string {
         tempBytes := make([]byte, 16)
         _, err := rand.Read(tempBytes)

         if err != nil {
                  fmt.Println("Error: ", err)
                  return nil
         }

         uuid := fmt.Sprintf("%X-%X-%X-%X-%X", tempBytes[0:4], tempBytes[4:6], tempBytes[6:8], tempBytes[8:10], tempBytes[10:])

         return &uuid
}

Test.go
package main

import (
 "crypto/rand"
 "fmt"
)

func GenerateRandomString() *string {
 tempBytes := make([]byte, 16)
 _, err := rand.Read(tempBytes)

 if err != nil {
  fmt.Println("Error: ", err)
  return nil
 }

 uuid := fmt.Sprintf("%X-%X-%X-%X-%X", tempBytes[0:4], tempBytes[4:6], tempBytes[6:8], tempBytes[8:10], tempBytes[10:])

 return &uuid
}


func main(){
 randomString := *GenerateRandomString()

 fmt.Println(randomString)
}

Output
D968C2BE-F2E0-9610-9EAE-991E808165E7



Previous                                                 Next                                                 Home

No comments:

Post a Comment