Detailed explanation of the bytes package in Golang (2): bytes.Reader

The bytes package in Golang is one of the IO operation standard libraries, which implements the operation on byte slices ([]byte) and provides functions similar to the strings package. This article first explains the bytes.Reader structure in the bytes package.

bytes.Reader

bytes.Reader implements the io.Reader, io.ReaderAt, io.WriterTo, io.Seeker, io.ByteScanner and io.RuneScanner interfaces, providing the function of reading data from byte slices. The structure definition and corresponding methods are as follows:

type Reader struct {
	s        []byte
	i        int64 // current reading index
	prevRune int   // index of previous rune; or < 0
}

Here are the methods provided by bytes.Reader:

  • func (r *Reader) Len() int, returns the number of unread bytes in the byte slice.
  • func (r *Reader) Read(b []byte) (n int, err error), read data from bytes.Reader and fill it into b bytes slice.
  • func (r *Reader) ReadAt(b []byte, off int64) (n int, err error), like Read, but uses offset off to specify where to start reading.
  • func (r *Reader) ReadByte() (byte, error), read a byte from the byte slice and return.
  • func (r *Reader) ReadRune() (ch rune, size int, err error), reads a UTF-8 encoded character from a byte slice, and returns the Unicode code point and character length of the character.
  • func (r *Reader) Seek(offset int64, whence int) (int64, error), move the read pointer from the byte slice, offset represents the offset, and whence represents the reference position of the movement.
  • func (r *Reader) UnreadByte() error, undoes the last read operation and moves the read pointer back one byte.
  • func (r *Reader) UnreadRune() error, undoes the last read operation and moves the read pointer back one UTF-8 character.
  • func (r *Reader) Size() int64, returns the length of the raw byte slice.
  • func (r *Reader) Reset(b []byte), reset Reader to read data from b
  • func (r *Reader) WriteTo(w io.Writer), write data into w until it is finished.

Example of use

package main

import (
	"bytes"
	"fmt"
)

func main() {
	data := []byte("路多辛的所思所想")
	reader := bytes.NewReader(data)

	// 读取整个字节数组
	buf := make([]byte, len(data))
	_, err := reader.Read(buf)
	if err != nil {
		fmt.Println("Read failed:", err)
	}
	fmt.Println("Bytes read:", buf)

	// 读取字节切片的一部分
	part := make([]byte, 3)
	_, err = reader.Read(part)
	if err != nil {
		fmt.Println("Read failed:", err)
	}
	fmt.Println("Bytes read:", part)

	// 查找并读取字节切片中的某个字符
	offset, err := reader.Seek(6, 0)
	if err != nil {
		fmt.Println("Seek failed:", err)
	}
	ch, size, err := reader.ReadRune()
	if err != nil {
		fmt.Println("ReadRune failed:", err)
	}
	fmt.Printf("Read %c with size %d at offset %d\n", ch, size, offset)
}

First, a byte slice data is defined, which is passed as a parameter to the bytes.NewReader function to create a reader object. Then use the Read method of the reader object to read the entire byte slice and a part of the data, and use the Seek and ReadRune methods to find a character in the byte slice and read it.

summary

bytes.Reader can easily process and read byte slices, and can read data in byte slices like reading files, which is very powerful and practical.

Guess you like

Origin blog.csdn.net/luduoyuan/article/details/131464806