liveBook · Manning

Bufio.ScanLines

As explained in Go in Practice, Second Edition

bufio.ScanLines is a Go bufio split function that returns successive lines from a byte stream for use with a scanner.

bufio.ScanLines provides a practical way to process newline-delimited files without loading the entire file into memory. Open the file, create a bufio.Scanner, select ScanLines as its split function, and handle each scanned line in a loop. The pattern suits JSONS, CSV, and other line-delimited formats; malformed JSON can be logged or skipped while processing continues. section 7.1.1

Listing 7.1 Outputting contents of a file
package main

import (
    "log"
    "os"
)

func main() {
    data, err := os.ReadFile("myfile.txt")     #1
    if err != nil {
        panic(err)
    }
    log.Println(string(data))      #2
}

Listing 7.4 shows the complete Go pattern: open structured.log, create a Scanner, set bufio.ScanLines, unmarshal scan.Text(), and continue after invalid JSON. section 7.1.1

How Go in Practice, Second Edition covers it

How deep it goes
Implements Scanner setup, split-function selection, iteration, and per-line JSON handling, while deferring larger parallel optimizations.
Examples
Demonstrates scanning structured.log containing newline-delimited JSON records and reporting each record's level.
What you can do after
Apply it via bufio.Scanner to process large line-delimited files incrementally and handle invalid JSON without stopping.
How it connects
Connects buffered I/O with file access, bufio.Scanner, JSON unmarshalling, CSV, and other line-delimited formats.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.