I'm learning for Go bufio package, and confused by a code snippet below:
1 s1 := strings.NewReader(strings.Repeat("a", 16) + strings.Repeat("b", 16))
2 r := bufio.NewReaderSize(s1, 16)
3 b, _ := r.Peek(3)
4 fmt.Printf("%q\n", b)
5 r.Read(make([]byte, 16))
6 r.Read(make([]byte, 15))
7 fmt.Printf("%q\n", b)
// "aaa"
// "bbb"
Isn't that r
in line 2 an empty []byte? Why can user r.Peek(3)
to figure out the "aaa" result?
Assume the bufio.NewReaderSize(s1, 16)
can Read 16 bytes from Reader s1
, this make line 3 reasonable; Why use twice r.Read() in line5 and line6?
And Isn't that the undelying array of r
in line 5 is "aaaaaaaaaaaaaaaa", and line6 became the "bbbbbbbbbbbbbbb"?
Or maybe the underlying array of r
always be "aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbb"?
If you can give me any inspiration, Thank you for your help.