Skip to content

Instantly share code, notes, and snippets.

@axiaoxin
Last active December 5, 2023 08:25
Show Gist options
  • Save axiaoxin/7a7fc42efd5fc0141470b49c9b82e570 to your computer and use it in GitHub Desktop.
Save axiaoxin/7a7fc42efd5fc0141470b49c9b82e570 to your computer and use it in GitHub Desktop.
使用golang实现一个函数,功能是生成一串6位数的随机数字字符串
/*使用golang实现一个函数,功能是生成一串6位数的随机数字字符串*/
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
digits := []rune("0123456789")
seed := rand.NewSource(time.Now().UnixNano()) // create a random number generator with the seed
rng := rand.New(seed)
// create a slice to store the result
keyLen := 6
result := make([]rune, keyLen)
// loop n times and append a random digit to the result
for i := range result {
result[i] = digits[rng.Intn(len(digits))]
}
fmt.Println(string(result))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment