github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/crypto/rand/example_test.go (about)

     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  	"io"
    12  )
    13  
    14  // This example reads 10 cryptographically secure pseudorandom numbers from
    15  // rand.Reader and writes them to a byte slice.
    16  func ExampleRead() {
    17  	c := 10
    18  	b := make([]byte, c)
    19  	n, err := io.ReadFull(rand.Reader, b)
    20  	if n != len(b) || err != nil {
    21  		fmt.Println("error:", err)
    22  		return
    23  	}
    24  	// The slice should now contain random bytes instead of only zeroes.
    25  	fmt.Println(bytes.Equal(b, make([]byte, c)))
    26  
    27  	// Output:
    28  	// false
    29  }