Golang bufio.Peek() function example

package bufio

Peek returns the next n bytes without advancing the reader. The bytes stop being valid at the next read call. If Peek returns fewer than n bytes, it also returns an error explaining why the read is short. The error is ErrBufferFull if n is larger than b's buffer size.

Golang bufio.Peek() function example

 readbuffer := bytes.NewBuffer([]byte("12345678"))
 reader := bufio.NewReader(readbuffer)
 peekbuffer, _ := reader.Peek(4)
 fmt.Println(string(peekbuffer))

Output :

1234

Advertisement