golang random string and int

前端之家收集整理的这篇文章主要介绍了golang random string and int前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main

import (
    "fmt"
    "math/rand"
    "time"
    "bytes"
)

func main() {
    fmt.Println(randomString(10) )
}


func randomString (l int ) string         {
    var result   bytes.Buffer
    var temp string
    for i:=0 ; i<l ;  {
        if string(randInt(65,90))!=temp {
        temp = string(randInt(65,90))
        result.WriteString(temp)
        i++
      }
    }
return result.String()
}

func randInt(min int,max int) int {
        rand.Seed( time.Now().UTC().UnixNano())
        return min + rand.Intn(max-min)
}

猜你在找的Go相关文章