golang生成验证码

//生成验证码
func randomKey2(length, level int) (strPol string) {
	var slicePol []byte
	var str string
	switch level {
	case 1:
		str = "0123456789"
	default:
		str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"
	}
	rand.Seed(time.Now().UnixNano())
	for i := 0; i < length; i++ {
		n := rand.Intn(len(str))
		slicePol = append(slicePol, str[n])
	}
	strPol = string(slicePol)
	return
}

func main() {
	str := randomKey2(6, 1)
	fmt.Println(str)
}

 

posted @ 2022-03-09 17:16  dddpppqqq  阅读(623)  评论(1编辑  收藏  举报