當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。