golang(go1.12.9) 的标准包bytes.Buffer,是一个简单的字节(byte) 缓冲buffer,
定义了一个默认的最小容量smallBufferSize = 64,实现了byte的读,写,清空等操作,
其中写操作稍微麻烦,需要考虑扩容的情形,下面主要分析一下buffer 扩容
首先,看一下buffer结构体的定义
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.
}