...

Source file src/crypto/rand/example_test.go

Documentation: crypto/rand

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package rand_test
     6  
     7  import (
     8  	"bytes"
     9  	"crypto/rand"
    10  	"fmt"
    11  )
    12  
    13  // This example reads 10 cryptographically secure pseudorandom numbers from
    14  // rand.Reader and writes them to a byte slice.
    15  func ExampleRead() {
    16  	c := 10
    17  	b := make([]byte, c)
    18  	_, err := rand.Read(b)
    19  	if err != nil {
    20  		fmt.Println("error:", err)
    21  		return
    22  	}
    23  	// The slice should now contain random bytes instead of only zeroes.
    24  	fmt.Println(bytes.Equal(b, make([]byte, c)))
    25  
    26  	// Output:
    27  	// false
    28  }
    29  

View as plain text