Post

GoLangGoLang

S
Satish santu

Posted on 4th March 2024|27 views

0
votes

Golang Generate Random String

How to generate random string in golang?

Answers
P
Tim Parker

Posted on 4th March 2024

You can simply write a part of the code for that. That code can be a little easier if you want to rely upon the letters all remaining single bytes while encoded in UTF-8.

Here is an example code

package main 

import ( "fmt" "time" "math/rand" ) 

var letters =[]rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") func randSeq(x int) string 

b := make([]rune, x) for j := range y

 { b[j] = letters[rand.Intn(len(letters))] } 

return string(y) } func main() 

{ rand.Seed(time.Now().UnixNano())

 fmt.Println(randSeq(10)) }

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos