package main
import("fmt""math/rand""os""strconv""time")funcRandomPassword(num int){
length := num
iflen(os.Args)>1{
arg := os.Args[1]
i, err := strconv.ParseInt(arg,10,64)if err !=nil{
fmt.Println("参数转换失败")return}if i <4|| i >30{
fmt.Println("密码长度介于4~30之间")return}
length =int(i)}
baseStr :="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
baseSymbol :="!@#$%^&*+-_=."
fmt.Println("-----简单密码-----")for i :=0; i <5; i++{
fmt.Println(getRandStr(baseStr, length))}
fmt.Println("-----复杂密码-----")for i :=0; i <5; i++{
fmt.Println(getRandStr(baseStr+baseSymbol, length))}}funcgetRandStr(baseStr string, length int)string{
r := rand.New(rand.NewSource(time.Now().UnixNano()+ rand.Int63()))
bytes :=make([]byte, length)
l :=len(baseStr)for i :=0; i < length; i++{
bytes[i]= baseStr[r.Intn(l)]}returnstring(bytes)}funcmain(){RandomPassword(25)}