How to Resume Reading a File from a Specific Line Using the Scanner in Go?

Barbara Streisand
Release: 2024-11-06 04:15:02
Original
316 people have browsed it

How to Resume Reading a File from a Specific Line Using the Scanner in Go?

Reading a File from a Specific Line Number Using Scanner

When working with files, it's often necessary to resume reading a file from a specific line number. The Scanner type in the bufio package can be extended to support this functionality.

Custom Split Function

The problem with using Scanner lies in its lack of line positioning capabilities. However, we can create a custom SplitFunc, which splits the input based on the newline character, and returns the count of characters read to maintain the file position.

func scanLinesWithPosition(data []byte, atEOF bool) (advance int, token []byte, err error) {
    advance, token, err = bufio.ScanLines(data, atEOF)
    pos += int64(advance)
    return
}
Copy after login

This SplitFunc increments a position variable with the number of characters read.

Modified Scanner

Next, we need to modify the Scanner to use our custom SplitFunc.

func withScannerAtPosition(input io.ReadSeeker, start int64) error {
    fmt.Println("--SCANNER, start:", start)
    if _, err := input.Seek(start, 0); err != nil {
        return err
    }
    scanner := bufio.NewScanner(input)
    scanner.Split(scanLinesWithPosition)

    for scanner.Scan() {
        fmt.Printf("Pos: %d, Scanned: %s\n", pos, scanner.Text())
    }
    return scanner.Err()
}
Copy after login

Example Usage

In this example, we read a sample file and print its contents with line numbers:

func main() {
    input := strings.NewReader("first\r\nsecond\nthird\nfourth")
    if err := withScannerAtPosition(input, 0); err != nil {
        fmt.Println("Scanner error:", err)
    }
}
Copy after login

Output:

--SCANNER, start: 0
Pos: 7, Scanned: first
Pos: 14, Scanned: second
Pos: 20, Scanned: third
Pos: 26, Scanned: fourth
Copy after login

The above is the detailed content of How to Resume Reading a File from a Specific Line Using the Scanner in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!