10 examples of 'golang generate random string' in Go

Every line of 'golang generate 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
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}
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

25func GenerateString(length int) string {
26 const charset = "abcdefghijklmnopqrstuvwxyz" +
27 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
28 var seededRand *rand.Rand = rand.New(
29 rand.NewSource(time.Now().UnixNano()))
30
31 b := make([]byte, length)
32 for i := range b {
33 b[i] = charset[seededRand.Intn(len(charset))]
34 }
35 return string(b)
36}
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}
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}
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}
63func GenerateRandomString() string {
64 length := 32
65 rand.Seed(time.Now().UTC().UnixNano())
66 const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
67 result := make([]byte, length)
68 for i := 0; i < length; i++ {
69 result[i] = chars[rand.Intn(len(chars))]
70 }
71 return string(result)
72}
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}
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}
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}
40func GenerateRandomString(args RandomStringArgs) string {
41 var r intner
42 if args.R != nil {
43 r = args.R
44 } else {
45 r = R
46 }
47 runes := args.Runes
48 if len(runes) == 0 {
49 runes = []rune(Base64Runes)
50 }
51 n := r.Intn(args.MaxLength-args.MinLength) + args.MinLength
52 ret := make([]rune, n)
53 for i := range ret {
54 ret[i] = runes[r.Intn(len(runes))]
55 }
56 return string(ret)
57}

Related snippets