github.com/pavlo67/common@v0.5.3/common/strlib/random.go (about)

     1  package strlib
     2  
     3  import (
     4  	"math/rand"
     5  	"time"
     6  )
     7  
     8  const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
     9  
    10  var r = rand.New(rand.NewSource(time.Now().UnixNano()))
    11  
    12  func RandomString(strlen int) string {
    13  	if strlen < 1 {
    14  		return ""
    15  	}
    16  
    17  	result := make([]byte, strlen)
    18  
    19  	for i := range result {
    20  		result[i] = chars[r.Intn(len(chars))]
    21  	}
    22  
    23  	return string(result)
    24  }