Created
March 21, 2014 07:29
-
-
Save hubsgz/9681334 to your computer and use it in GitHub Desktop.
go 生成随机字符串
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//生成随机字符串 | |
func RandStr(strlen int) string { | |
rand.Seed(time.Now().Unix()) | |
data := make([]byte, strlen) | |
var num int | |
for i := 0; i < strlen; i++ { | |
num = rand.Intn(57) + 65 | |
for { | |
if num>90 && num<97 { | |
num = rand.Intn(57) + 65 | |
} else { | |
break | |
} | |
} | |
data[i] = byte(num) | |
} | |
return string(data) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rand.Seed(time.Now().Unix()) --> rand.Seed(time.Now().UnixNano()), 否则调用频繁的时候回返回同一个值