10 examples of 'golang random string' in Go

Every line of 'golang random string' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
67func RandomString() string {
68 suffix := make([]byte, randSuffixLen)
69
70 for i := range suffix {
71 suffix[i] = letterBytes[rand.Intn(len(letterBytes))]
72 }
73 return string(suffix)
74}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

29func GenerateRandString(length int) string {
30 r := rand.New(rand.NewSource(time.Now().Unix()))
31 bytes := make([]byte, length)
32 for i := 0; i < length; i++ {
33 b := r.Intn(26) + 65
34 bytes[i] = byte(b)
35 }
36 return strings.ToLower(string(bytes))
37}
97func randomString() (string, error) {
98 buf := make([]byte, 32)
99 _, err := rand.Read(buf)
100 if err != nil {
101 return "", err
102 }
103 str := base64.StdEncoding.EncodeToString(buf)
104 return str, nil
105}
76func RandomString(strlen int) string {
77 rand.Seed(time.Now().UTC().UnixNano())
78 const chars = "abcdefghijklmnopqrstuvwzyz0123456789/"
79 result := make([]byte, strlen)
80 for i := 0; i < strlen; i++ {
81 result[i] = chars[rand.Intn(len(chars))]
82 }
83 return string(result)
84}
230func randomString(length int) string {
231 var rng, offset, mode int;
232
233 str := make([]int, length);
234 for i := 0; i < length; i++ {
235 mode = rand.Intn(3);
236
237 if mode == 0 {
238 rng, offset = 26, 65
239 } else if mode == 1 {
240 rng, offset = 26, 97
241 } else {
242 rng, offset = 10, 48
243 }
244
245 str[i] = rand.Intn(rng) + offset;
246 }
247
248 return string(str);
249}
78func RandomString(length int) string {
79 src := rand.NewSource(time.Now().UnixNano())
80 b := make([]byte, length)
81 for i, cache, remain := length-1, src.Int63(), letterIndexMax; i >= 0; {
82 if remain == 0 {
83 cache, remain = src.Int63(), letterIndexMax
84 }
85 if idx := int(cache & letterIndexMask); idx < len(letterBytes) {
86 b[i] = letterBytes[idx]
87 i--
88 }
89 cache >>= letterIndexBits
90 remain--
91 }
92
93 return string(b)
94}
90func RandString(length int) string {
91 return stringWithCharset(length, charset)
92}
12func randString() string {
13 return adjectives[rand.Int()%len(adjectives)] + " " + names[rand.Int()%len(names)]
14}
27func randomString(l int) string {
28 bytes := make([]byte, l)
29 for i := 0; i < l; i++ {
30 bytes[i] = byte(randInt(65, 90))
31 }
32 return string(bytes)
33}
112func (m *Misc) RandString(strlen int) string {
113 codes := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
114 codeLen := len(codes)
115 data := make([]byte, strlen)
116 rand.Seed(time.Now().UnixNano() + rand.Int63() + rand.Int63() + rand.Int63() + rand.Int63())
117 for i := 0; i < strlen; i++ {
118 idx := rand.Intn(codeLen)
119 data[i] = byte(codes[idx])
120 }
121 return string(data)
122}

Related snippets