Standard library / crypto/rand

crypto/rand.Read()

Fill a buffer using the secure random Reader. In Go 1.23.2, returns full length exactly when err is nil; do not use partial bytes after failure. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func Read(b []byte) (n int, err error)

Parameters & Attributes

NameTypeDescription
b[]byteDestination for random bytes.

Return Value

(n int, err error)

Bytes read and any error.

Working Example

Example Snippetgo
package main
import (  "crypto/rand"  "fmt")
func main() {  b := make([]byte, 32)  n, e := rand.Read(b)  if e != nil {    panic(e)  }  fmt.Println(n == len(b))}
Go Reference