Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท bytes, unicode and text

Go bytes.Buffer type

Reference for Go bytes.Buffer: write and read bytes and strings, use it as an io.Writer and io.Reader, and learn Len, Reset, Truncate and Next behavior.

bytes.Buffer is a variable-sized buffer of bytes with both read and write methods. You write to the end, you read from the front, and the buffer grows as needed. Its zero value is an empty buffer ready to use.

It implements io.Writer, io.Reader, io.ByteReader, io.ByteWriter, io.StringWriter, io.WriterTo and io.ReaderFrom, so it plugs into fmt.Fprintf, json.NewEncoder, io.Copy and most other I/O code. It is the usual choice for capturing output in tests or building a payload that you will later read or send.

Go
var buf bytes.Buffer
buf.WriteString("hello ")
fmt.Fprintf(&buf, "%d items", 3)
fmt.Println(buf.String())

Try it

Go Writing, then reading

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Capturing output and peeking

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
func NewBuffer(buf []byte) *Buffer
func NewBufferString(s string) *Buffer

func (b *Buffer) Write(p []byte) (n int, err error)
func (b *Buffer) WriteString(s string) (n int, err error)
func (b *Buffer) WriteByte(c byte) error
func (b *Buffer) WriteRune(r rune) (n int, err error)
func (b *Buffer) Read(p []byte) (n int, err error)
func (b *Buffer) ReadString(delim byte) (string, error)
func (b *Buffer) ReadBytes(delim byte) ([]byte, error)
func (b *Buffer) ReadByte() (byte, error)
func (b *Buffer) ReadRune() (r rune, size int, err error)
func (b *Buffer) Next(n int) []byte
func (b *Buffer) Bytes() []byte
func (b *Buffer) String() string
func (b *Buffer) Len() int
func (b *Buffer) Cap() int
func (b *Buffer) Grow(n int)
func (b *Buffer) Truncate(n int)
func (b *Buffer) Reset()
Method Description
Write* Append to the end. Errors are always nil; if the buffer gets too large it panics with bytes.ErrTooLarge.
Read*, Next Take bytes from the front and remove them. Reading an empty buffer returns io.EOF.
Bytes, String The unread portion, without consuming it. Bytes shares the buffer's memory.
Len The number of unread bytes.
Truncate(n) Keeps the first n unread bytes; panics if n is out of range.
Reset Empties the buffer but keeps its memory for reuse.

Usage notes

  • Reads consume. After io.ReadAll(&buf) the buffer is empty. Call buf.String() or buf.Bytes() if you only want to look.
  • Pass a pointer. Methods have pointer receivers; use &buf when handing it to fmt.Fprintf and other functions.
  • Bytes() aliases the buffer. The slice is valid only until the next write or read; copy it with bytes.Clone to keep it.
  • A nil *Buffer prints as <nil> from String() rather than panicking; other methods on nil panic.
  • NewBuffer(buf) takes ownership of the slice; do not use buf afterwards. Its contents become the initial unread data.
  • Not safe for concurrent use. Protect it with a mutex.
  • For string-only building use strings.Builder, which can hand over the string without a copy; Buffer.String() copies.