type Buffer
A Buffer is a variable-sized buffer of bytes with Read and Write methods. The zero value for Buffer is an empty buffer ready to use.
Buffer是一个可变大小的字节缓冲区,有读和写的方法。Buffer的零值是一个准备使用的空缓冲区。
Buffer.Write
func (b *Buffer) WriteByte(c byte) error
WriteByte appends the byte c to the buffer, growing the buffer as needed. The returned error is always nil, but is included to match bufio.Writer\’s WriteByte. If the buffer becomes too large, WriteByte will panic with ErrTooLarge.
WriteByte将字节c追加到缓冲区,根据需要增加缓冲区。返回的错误总是nil,但被包括在内以匹配bufio.Writer的WriteByte。如果缓冲区变得太大,WriteByte将以ErrTooLarge惊慌失措。
Buffer.Len
返回一个buffer的长度
Buffer.String
buffer转成一个字符串
bytes.NewBuffer
func NewBuffer(buf []byte) *Buffer
NewBuffer creates and initializes a new Buffer using buf as its initial contents. The new Buffer takes ownership of buf, and the caller should not use buf after this call. NewBuffer is intended to prepare a Buffer to read existing data. It can also be used to set the initial size of the internal buffer for writing. To do that, buf should have the desired capacity but a length of zero.
In most cases, new(Buffer) (or just declaring a Buffer variable) is sufficient to initialize a Buffer.
NewBuffer创建并初始化一个新的Buffer,使用buf作为其初始内容。新的Buffer拥有buf的所有权,调用者在这次调用后不应该再使用buf。NewBuffer是用来准备一个Buffer来读取现有的数据。它也可以用来设置内部缓冲区的初始大小,以便写入。要做到这一点,buf应该有理想的容量,但长度为0。
在大多数情况下,new(Buffer)(或者只是声明一个Buffer变量)就足以初始化一个Buffer。