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

bufio.Reader

bufio.Reader is an implementation of the io.Reader interface with a buffer, providing a series of methods to help read data. Using bufio.Reader can reduce I/O operations and reduce the time and resource overhead of reading data. The main feature is that it will store the data read from the underlying io.Reader in memory, and then read the data from the memory buffer first, which can reduce the number of accesses to the underlying io.Reader object and reduce the pressure on the operating system. The structure definition and corresponding methods are as follows:

type Reader struct {
	buf          []byte
	rd           io.Reader // reader provided by the client
	r, w         int       // buf read and write positions
	err          error
	lastByte     int // last byte read for UnreadByte; -1 means invalid
	lastRuneSize int // size of last rune read for UnreadRune; -1 means invalid
}

Here are some main methods of bufio.Reader:

  • func (b *Reader) Read(p []byte) (n int, err error): Read data from the buffer into p, return the number of bytes read and possible read errors. If the length of p is greater than the size of the buffer, the expansion operation of the buffer will be triggered.
  • func (b *Reader) ReadByte() (byte, error): Read a byte from the buffer and return the byte and possible error messages.
  • func (b *Reader) ReadRune() (r rune, size int, err error): Read a UTF-8 encoded character from the buffer, return the character and possible errors. Returns an error if there are not enough bytes in the buffer to represent a full UTF-8 character.
  • func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error): Read a line from the buffer, and return the line content and possible errors.

I won’t explain the other methods one by one. It’s best to see and use them yourself to experience them.

Advantage

bufio.Reader provides a buffered read operation, which first stores the data read through the system call in the memory, and then reads the data from the memory buffer, which greatly reduces the number of system calls and reduces the pressure on the operating system. Speed ​​up data reading.

bufio.Reader provides many types of reading methods, such as ReadByte(), ReadRune() and ReadLine(), etc., which are very convenient to use.

Example of use

A simple usage example is as follows:

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	file, err := os.Open("file.txt")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	reader := bufio.NewReader(file)
	buffer := make([]byte, 4)

	for {
		n, err := reader.Read(buffer)
		if err != nil {
			break
		}
		fmt.Print(string(buffer[:n]))
	}
}

Use the NewReader() method to create a bufio.Reader instance, then create a buffer buffer, and use the Read() method to read data from the buffer in a loop.

summary

bufio.Reader provides buffered read operation and rich read operation methods, especially when reading large blocks of data, using bufio.Reader can significantly improve the performance and response speed of the program.

Guess you like

Origin blog.csdn.net/luduoyuan/article/details/131334553
Recommended