慕勒3428872
您的期望不是基于任何记录在案的bufio.Reader. 如果您希望“读取始终提供字节数 len(byteArray)”,则必须使用io.ReadAtLeast。package mainimport ( "bufio" "fmt" "io" "strings")const bufSize = 10const blockSize = 12func 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