Post

golangでランダム値を算出

解決

import ( "math/rand" "time" ) const charset = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" var seededRand *rand.Rand = rand.New( rand.NewSource(time.Now().UnixNano())) func StringWithCharset(length int, charset string) string { b := make([]byte, length) for i := range b { b[i] = charset[seededRand.Intn(len(charset))] } return string(b) } func String(length int) string { return StringWithCharset(length, charset) }

Creating Random Strings in Go - Calhoun.io

事象

Rubyの関数のようなランダム値をgoで算出したい。

p SecureRandom.hex(10) #=> "52750b30ffbc7de3b362"

module SecureRandom (Ruby 2.7.0 リファレンスマニュアル)

This post is licensed under CC BY 4.0 by the author.