How to generate a random string of a fixed length

2019-01-04 04:27发布

I want a random string of characters only (uppercase or lowercase), no numbers, in Go. What is the fastest and simplest way to do this?

标签: string random go
8条回答
老娘就宠你
2楼-- · 2019-01-04 05:11

in addition I've found a package which have bunch of methods to manipulate fake data. Found it useful for seeding database while developing https://github.com/Pallinder/go-randomdata. Might be helpful to someone else as well

查看更多
你好瞎i
3楼-- · 2019-01-04 05:22

Here is my way ) Use math rand or crypto rand as you wish.

func randStr(len int) string {
    buff := make([]byte, len)
    rand.Read(buff)
    str := base64.StdEncoding.EncodeToString(buff)
    // Base 64 can be longer than len
    return str[:len]
}
查看更多
登录 后发表回答