Golang之bytes.buffer

bytes.buffer是一个缓冲byte类型的缓冲器存放着都是byte
Buffer 是 bytes 包中的一个 type Buffer struct{…}

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,具有 Read 和Write 方法。 Buffer 的 零值 是一个 空的 buffer,但是可以使用)
Buffer 就像一个集装箱容器,可以存东西,取东西(存取数据)

  • 创建 一个 Buffer (其实底层就是一个 []byte, 字节切片)
  • 向其中写入数据 (Write mtheods)
  • 从其中读取数据 (Write methods)

Read

给Read方法一个容器p,读完后,p就满了,缓冲器相应的减少了,返回的n为成功读的数量

// Read reads the next len(p) bytes from the buffer or until the buffer
// is drained.  The return value n is the number of bytes read.  If the
// buffer has no data to return, err is io.EOF (unless len(p) is zero);
// otherwise it is nil.
func (b *Buffer) Read(p []byte) (n int, err error) {}
func Read(){
    bufs := bytes.NewBufferString("Learning swift.")
    fmt.Println(bufs.String())

    //声明一个空的slice,容量为8
    l := make([]byte, 8)
    //把bufs的内容读入到l内,因为l容量为8,所以只读了8个过来
    bufs.Read(l)
    fmt.Println("::bufs缓冲器内容::")
    fmt.Println(bufs.String())
    //空的l被写入了8个字符,所以为 Learning
    fmt.Println("::l的slice内容::")
    fmt.Println(string(l))
    //把bufs的内容读入到l内,原来的l的内容被覆盖了
    bufs.Read(l)
    fmt.Println("::bufs缓冲器被第二次读取后剩余的内容::")
    fmt.Println(bufs.String())
    fmt.Println("::l的slice内容被覆盖,由于bufs只有7个了,因此最后一个g被留下来了::")
    fmt.Println(string(l))

}

打印:

=======Read=======
Learning swift.
::bufs缓冲器内容::
swift.
::l的slice内容::
Learning
::bufs缓冲器被第二次读取后剩余的内容::

::l的slice内容被覆盖::
swift.g

ReadByte

返回缓冲器头部的第一个byte,缓冲器头部第一个byte被拿掉

// ReadByte reads and returns the next byte from the buffer.
// If no byte is available, it returns error io.EOF.
func (b *Buffer) ReadByte() (c byte, err error) {}
func ReadByte(){
    bufs := bytes.NewBufferString("Learning swift.")
    fmt.Println(bufs.String())
    //读取第一个byte,赋值给b
    b, _ := bufs.ReadByte()
    fmt.Println(bufs.String())
    fmt.Println(string(b))
}

打印:

=======ReadByte===
Learning swift.
earning swift.
L

ReadRune

ReadRune和ReadByte很像
返回缓冲器头部的第一个rune,缓冲器头部第一个rune被拿掉

// ReadRune reads and returns the next UTF-8-encoded
// Unicode code point from the buffer.
// If no bytes are available, the error returned is io.EOF.
// If the bytes are an erroneous UTF-8 encoding, it
// consumes one byte and returns U+FFFD, 1.
func (b *Buffer) ReadRune() (r rune, size int, err error) {}
func ReadRune(){
    bufs := bytes.NewBufferString("学swift.")
    fmt.Println(bufs.String())

    //读取第一个rune,赋值给r
    r,z,_ := bufs.ReadRune()
    //打印中文"学",缓冲器头部第一个被拿走
    fmt.Println(bufs.String())
    //打印"学","学"作为utf8储存占3个byte
    fmt.Println("r=",string(r),",z=",z)

}

ReadBytes

ReadBytes需要一个byte作为分隔符,读的时候从缓冲器里找第一个出现的分隔符(delim),找到后,把从缓冲器头部开始到分隔符之间的所有byte进行返回,作为byte类型的slice,返回后,缓冲器也会空掉一部分

// ReadBytes reads until the first occurrence of delim in the input,
// returning a slice containing the data up to and including the delimiter.
// If ReadBytes encounters an error before finding a delimiter,
// it returns the data read before the error and the error itself (often io.EOF).
// ReadBytes returns err != nil if and only if the returned data does not end in
// delim.
func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) {}
func ReadBytes(){
    bufs := bytes.NewBufferString("现在开始 Learning swift.")
    fmt.Println(bufs.String())

    var delim byte = "L"
    line, _ := bufs.ReadBytes(delim)
    fmt.Println(bufs.String())
    fmt.Println(string(line))
}

打印:

=======ReadBytes==
现在开始 Learning swift.
earning swift.
现在开始 L

ReadString

ReadString需要一个byte作为分隔符,读的时候从缓冲器里找第一个出现的分隔符(delim),找到后,把从缓冲器头部开始到分隔符之间的所有byte进行返回,作为字符串,返回后,缓冲器也会空掉一部分
和ReadBytes类似

// ReadString reads until the first occurrence of delim in the input,
// returning a string containing the data up to and including the delimiter.
// If ReadString encounters an error before finding a delimiter,
// it returns the data read before the error and the error itself (often io.EOF).
// ReadString returns err != nil if and only if the returned data does not end
// in delim.
func (b *Buffer) ReadString(delim byte) (line string, err error) {}

ReadFrom

从一个实现io.Reader接口的r,把r里的内容读到缓冲器里,n返回读的数量

// ReadFrom reads data from r until EOF and appends it to the buffer, growing
// the buffer as needed. The return value n is the number of bytes read. Any
// error except io.EOF encountered during the read is also returned. If the
// buffer becomes too large, ReadFrom will panic with ErrTooLarge.
func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {}
func ReadFrom(){
    //test.txt 内容是 "未来"
    file, _ := os.Open("learngo/bytes/text.txt")
    buf := bytes.NewBufferString("Learning swift.")
    buf.ReadFrom(file)              //将text.txt内容追加到缓冲器的尾部
    fmt.Println(buf.String())
}

打印:

=======ReadFrom===
Learning swift.未来

Reset

将数据清空,没有数据可读

// Reset resets the buffer so it has no content.
// b.Reset() is the same as b.Truncate(0).
func (b *Buffer) Reset() { b.Truncate(0) }
func Reset(){
    bufs := bytes.NewBufferString("现在开始 Learning swift.")
    fmt.Println(bufs.String())

    bufs.Reset()
    fmt.Println("::已经清空了bufs的缓冲内容::")
    fmt.Println(bufs.String())
}

打印:

=======Reset======
现在开始 Learning swift.
::已经清空了bufs的缓冲内容::

string

将未读取的数据返回成 string

// String returns the contents of the unread portion of the buffer
// as a string.  If the Buffer is a nil pointer, it returns "<nil>".
func (b *Buffer) String() string {}
文章导航