I have a byte array, with a fixed length of 4.
token := make([]byte, 4)
I need to set each byte to a random byte. How can I do so, in the most
Go 1.6 added a new function to the math/rand package:
func Read(p []byte) (n int, err error)
which fills the passed byte
slice with random data. Using this rand.Read():
token := make([]byte, 4)
if _, err := rand.Read(token); err != nil {
// Handle err
}
fmt.Println(token)
rand.Read()
has 2 return values: the number of "read" bytes and an (optional) error. This is to conform with the general io.Reader interface, but the documentation of rand.Read()
states that (despite its signature) it will never actually return a non-nil
error, so we may omit checking it, which simplifies it to this:
token := make([]byte, 4)
rand.Read(token)
fmt.Println(token)
Don't forget to call rand.Seed() to properly initialize it before you use the math/rand
package, e.g.:
rand.Seed(time.Now().UnixNano())
Note: Prior to Go 1.6 there was no math/rand.Read()
function, but there was (and still is) a crypto/rand.Read() function, but the crypto/rand package implements a cryptographically secure pseudorandom number generator, so it is much slower than math/rand
.