2 回答
TA贡献1848条经验 获得超6个赞
您的期望不是基于任何记录在案的bufio.Reader. 如果您希望“读取始终提供字节数 len(byteArray)”,则必须使用io.ReadAtLeast。
package main
import (
"bufio"
"fmt"
"io"
"strings"
)
const bufSize = 10
const blockSize = 12
func main() {
s := strings.NewReader("some length test string buffer boom")
buffer := bufio.NewReaderSize(s, bufSize)
b := make([]byte, blockSize)
n, err := io.ReadAtLeast(buffer, b, blockSize)
if err != nil {
fmt.Println(err)
}
fmt.Printf("First read got %d bytes: %s\n", n, string(b))
d := make([]byte, blockSize)
n, err = io.ReadAtLeast(buffer, d, blockSize)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Second read got %d bytes: %s\n", n, string(d))
}
输出:
First read got 12 bytes: some length
Second read got 12 bytes: test string
- 2 回答
- 0 关注
- 247 浏览
添加回答
举报