Standard library / bufio

bufio.Reader.Peek()

Returns up to n upcoming bytes without advancing; result aliases internal storage and becomes invalid after the next read. Large n can return ErrBufferFull. Expected stdout: 'ab <nil>\na\n'.

Formal Syntax & Structure

go syntaxgo
func (b *Reader) Peek(n int) ([]byte, error)

Parameters & Attributes

NameTypeDescription
nintRequested byte count or bound.

Return Value

([]byte, error)

Returns up to n upcoming bytes without advancing; result aliases internal storage and becomes invalid after the next read.

Working Example

Example Snippetgo
package main
import (  "bufio"  "fmt"  "strings")
func main() {  r := bufio.NewReader(strings.NewReader("abc"))  p, e := r.Peek(2)  fmt.Println(string(p), e)  c, _ := r.ReadByte()  fmt.Println(string(c))}
Go Reference