Buffer
what is it
A Buffer is a variable-sized buffer of bytes with [Buffer.Read] and [Buffer.Write] methods.
- The zero value for Bufferis anempty bufferready 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.
- Readreads up to- len(p)bytes into- p
- It returns the number of bytesread(0 <= n <= len(p))and anyerrorencountered
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() }