当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


GO Read用法及代码示例

GO语言"crypto/rand"包中"Read"函数的用法及代码示例。

用法:

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

Read 是一个辅助函数,它使用 io ReadFull 调用 Reader.Read 在返回时,n == len(b) 当且仅当 err == nil 时。

例子:

此示例从 rand.Reader 读取 10 个加密安全伪随机数并将它们写入字节片。

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)))

}

输出:

false

相关用法


注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 Read。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。