Read in Go

Posted by GoDoc
Public (Editable by Users)

This example reads 10 cryptographically secure pseudorandom numbers from rand.Reader and writes them to a byte slice.

Go
Edit
package main

import (
	"bytes"
	"crypto/rand"
	"fmt"
)

func main() {
	c := 10
	b := make([]byte, c)
	_, err := rand.Read(b)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	// The slice should now contain random bytes instead of only zeroes.
	fmt.Println(bytes.Equal(b, make([]byte, c)))
}