github.com/OrigamiWang/msd/micro@v0.0.0-20240229032328-b62246268db9/util/rand/rand.go (about) 1 package main 2 3 import ( 4 "crypto/rand" 5 "encoding/hex" 6 "io" 7 ) 8 9 func GenerateRandomString(length int) string { 10 buf := make([]byte, length) 11 if _, err := io.ReadFull(rand.Reader, buf); err != nil { 12 return "" 13 } 14 keyHex := hex.EncodeToString(buf) 15 return keyHex 16 }