Home

bufioNewReader

bufio.NewReader is a constructor in the Go standard library that returns a new buffered reader wrapping an underlying io.Reader. Its primary purpose is to improve efficiency by buffering input reads, reducing the number of calls to the underlying source such as a file or network connection.

The canonical form is func NewReader(rd io.Reader) *Reader, which creates a Reader with a default internal buffer

A bufio.Reader fills its internal buffer from the underlying reader as needed. Read copies data from the

Notable limitations include that a bufio.Reader is not safe for concurrent use by multiple goroutines; external

size
of
4096
bytes.
For
cases
where
a
different
buffer
size
is
desired,
the
package
provides
func
NewReaderSize(rd
io.Reader,
size
int)
*Reader.
The
resulting
object
is
a
*bufio.Reader
that
exposes
methods
for
reading
data
from
the
buffered
source.
buffer
into
the
caller’s
slice
and,
if
necessary,
reads
more
data
from
the
underlying
reader
to
refill
the
buffer.
The
reader
also
offers
higher-level
operations
such
as
ReadBytes(delim
byte)
and
ReadString(delim
byte),
which
read
until
a
specified
delimiter,
and
ReadRune
or
ReadByte
for
single
units.
Peek
allows
inspecting
upcoming
bytes
without
advancing
the
reader,
and
Buffered
reports
how
many
bytes
are
currently
stored
in
the
internal
buffer.
The
Reset
method
can
reassign
the
reader
to
a
new
underlying
io.Reader.
synchronization
is
required
if
sharing
a
single
reader
across
goroutines.
bufio.NewReader
is
commonly
used
to
efficiently
parse
tokens,
lines,
or
records
from
files,
networks,
or
other
streams,
especially
when
small
reads
are
common
or
when
there
is
a
need
to
minimize
system
calls.