GO語言"io"包中"ReadAtLeast"函數的用法及代碼示例。
用法:
func ReadAtLeast(r Reader, buf []byte, min int)(n int, err error)
ReadAtLeast 從 r 讀取到 buf,直到它至少讀取了 min 字節。它返回複製的字節數,如果讀取的字節數較少,則返回錯誤。僅當未讀取任何字節時,該錯誤才是 EOF。如果在讀取少於 min 字節後發生 EOF,ReadAtLeast 返回 ErrUnexpectedEOF 如果 min 大於 buf 的長度,ReadAtLeast 返回 ErrShortBuffer 返回時,n >= min 當且僅當 err == nil .如果 r 返回讀取至少 min 字節的錯誤,則刪除該錯誤。
例子:
package main
import (
"fmt"
"io"
"log"
"strings"
)
func main() {
r := strings.NewReader("some io.Reader stream to be read\n")
buf := make([]byte, 14)
if _, err := io.ReadAtLeast(r, buf, 4); err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", buf)
// buffer smaller than minimal read size.
shortBuf := make([]byte, 3)
if _, err := io.ReadAtLeast(r, shortBuf, 4); err != nil {
fmt.Println("error:", err)
}
// minimal read size bigger than io.Reader stream
longBuf := make([]byte, 64)
if _, err := io.ReadAtLeast(r, longBuf, 64); err != nil {
fmt.Println("error:", err)
}
}
輸出:
some io.Reader error: short buffer error: unexpected EOF
相關用法
- GO ReadAll用法及代碼示例
- GO ReadMessage用法及代碼示例
- GO Read用法及代碼示例
- GO ReadFile用法及代碼示例
- GO Reader.Multistream用法及代碼示例
- GO Reader.Len用法及代碼示例
- GO Reader用法及代碼示例
- GO ReadDir用法及代碼示例
- GO ReadFull用法及代碼示例
- GO Reader.ReadAll用法及代碼示例
- GO Regexp.FindString用法及代碼示例
- GO Regexp.FindAllIndex用法及代碼示例
- GO ResponseRecorder用法及代碼示例
- GO ReverseBytes64用法及代碼示例
- GO ReverseBytes16用法及代碼示例
- GO Regexp.ReplaceAllLiteralString用法及代碼示例
- GO Regexp.FindStringSubmatch用法及代碼示例
- GO Regexp.FindAllString用法及代碼示例
- GO Regexp.ExpandString用法及代碼示例
- GO ResponseWriter用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 ReadAtLeast。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。