How Generate A Random String Of Symbols (Answered)

Hello mates. I previously created a topic[1] with the same title that I deleted. I am reposting it here with the answer.

[1] How Generate A Random String Of Symbols

Answer:

rand.Seed(time.Now().UnixNano())
digits := "0123456789"
specials := "~=+%^*/()[]{}/!@#$?|"
all := "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
    "abcdefghijklmnopqrstuvwxyz" +
    digits + specials
length := 8
buf := make([]byte, length)
buf[0] = digits[rand.Intn(len(digits))]
buf[1] = specials[rand.Intn(len(specials))]
for i := 2; i < length; i++ {
    buf[i] = all[rand.Intn(len(all))]
}
rand.Shuffle(len(buf), func(i, j int) {
    buf[i], buf[j] = buf[j], buf[i]
})
str := string(buf) 

Source: Generate a random string (password) · YourBasic Go

Obs: You can also add more symbols since they are ASCII (i. e. ´;:<> \" \\) (I put the slash in these last symbols to “declared” them as a string not as a attribute.)

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.