Home  >  Article  >  Backend Development  >  Read the file contents using the bufio.NewReader function in the Go language documentation

Read the file contents using the bufio.NewReader function in the Go language documentation

WBOY
WBOYOriginal
2023-11-03 20:07:461004browse

Read the file contents using the bufio.NewReader function in the Go language documentation

Use the NewReader function in the bufio package of the Go language to easily read the file content. Next, let's take a look at how to use this function to read files and perform some basic operations.

First, we need to import the bufio package:

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

The NewReader function creates a buffer reader of type bufio.Reader, which reads the file content line by line and caches it in memory. for subsequent reading.

We can read the file and print it line by line through the following code:

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

reader := bufio.NewReader(file)
for {
    line, err := reader.ReadString('
')
    if err == io.EOF {
        break
    }
    if err != nil {
        panic(err)
    }
    fmt.Print(line)
}

In the code, we first open a file named test.txt, and if an error occurs, exit directly program. Then, create a buffered reader through the NewReader function and associate it with the file.

Next, in an infinite loop, we can use the readString function to read the file content line by line and print each line on the screen. If the end-of-file character EOF is encountered, break out of the loop. Otherwise, if another error is encountered, an exception is thrown and the program exits.

In addition, we can also use the ReadBytes function to read bytes in the file and use the strings package to operate on these bytes. The following code shows how to convert the read content into a string and find the substrings in it:

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

reader := bufio.NewReader(file)

content, err := ioutil.ReadAll(reader)
if err != nil {
    panic(err)
}

str := string(content)
if strings.Contains(str, "Hello World") {
    fmt.Println("Found!")
}

In this code, we first read the contents of the entire file through the ReadAll function and Save as byte slice. We then convert the byte slice into a string and use the Contains function to find the "Hello World" substring. If found, output "Found!", otherwise do nothing.

Using the bufio package and corresponding functions, we can easily read the file content and operate on it. Not only that, this approach also helps us handle large files because it does not read the entire file into memory at once.

The above is the detailed content of Read the file contents using the bufio.NewReader function in the Go language documentation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn