各位大神可以帮忙解读下面的代码吗?菜鸟刚写,有的地方了解得不透
万分感激
```go
func main() {
sr := strings.NewReader("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
buf := bufio.NewReaderSize(sr, 0)
b := make([]byte, 10)
//返回缓存区,未读取的数据长度
fmt.Println(buf.Buffered())
s, _ := buf.Peek(5)
s[0], s[1], s[2] = 'a', 'b', 'c'
fmt.Printf("%d %q\n", buf.Buffered(), s)
buf.Discard(1)
fmt.Println(buf.Buffered())
for n, err := 0, error(nil); err == nil; {
n, err = buf.Read(b)
fmt.Printf("%q", b[:])
fmt.Printf("%d %q %v\n", buf.Buffered(), b[:n], err)
}
}
```
1. fmt.Println(buf.Buffered())这里为什么输出的是 "0"
2. fmt.Printf("%q", b[:])l输出的会有重复字符
3. fmt.Printf("%d %q %v\n", buf.Buffered(), b[:n], err) n返回字节数有时候为啥是0、5、6为何不是10呢?
这些问题在标准库的文档里面都可以找到答案:
`type Reader `
`If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more.`
`Callers should always process the n > 0 bytes returned before considering the error err. Doing so correctly handles I/O errors that happen after reading some bytes and also both of the allowed EOF behaviors.`
如果想把buf读满再返回,可以用io.ReadFull(), 返回nil表示读满了
`func ReadFull(r Reader, buf []byte) (n int, err error)`
#1