Home >Backend Development >Golang >How to Resume Reading a File from a Specific Line Using the Scanner in Go?

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

Barbara Streisand
Barbara StreisandOriginal
2024-11-06 04:15:02378browse

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
}

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()
}

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)
    }
}

Output:

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

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!

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