HomeToolsAbout

Buffer

what is it

A Buffer is a variable-sized buffer of bytes with [Buffer.Read] and [Buffer.Write] methods.

  • The zero value for Buffer is an empty buffer ready to use.
type Buffer struct { buf []byte // contents are the bytes buf[off : len(buf)] off int // read at &buf[off], write at &buf[len(buf)] lastRead readOp // last read operation, so that Unread* can work correctly. }

Create new buffer/byte array ([]byte)

io.Reader is an interface that wraps the basic Read method.

  • Read reads up to len(p) bytes into p
  • It returns the number of bytes read (0 <= n <= len(p)) and any error encountered
type Reader interface { Read(p []byte) (n int, err error) }

It is useful when

import ( "bytes" ) func main() []byte { buf := new(bytes.Buffer) return buf.Bytes() }
AboutContact