liveBook · Manning

Line-delimited record

As explained in Go in Practice, Second Edition

A line-delimited record is an independently represented data item terminated or separated by a newline.

A line-delimited record is an independently represented data item separated by a newline. This layout supports streaming: read one line, parse it, and continue without loading the entire file. section 7.1.1

Line-delimited records are useful when:

  • Each record is JSON, CSV, or another format that can be processed independently. section 7.1.1
  • Buffered I/O avoids scalability problems associated with reading a whole file at once. section 7.1.1
  • An invalid record can be logged or ignored while processing continues with later lines. section 7.1.1
Listing 7.3 Finding JSON needles in a haystack
    lineJSON := make(map[string]interface{})       #1
    var bChunk []byte
    for {
        b := make([]byte, 2)
        _, err := file.Read(b)

        if err != nil {
            break
        }

        bChunk = append(bChunk, b[0:]...)      #2

        if err := json.Unmarshal(bChunk, &lineJSON); 
        err == nil {                               #3
            log.Println(lineJSON)
            bChunk = []byte{}
        }
    }

The implementation opens a structured log, uses bufio.Scanner with bufio.ScanLines, unmarshals each line as JSON, and reports parsing errors without stopping the scan. section 7.1.1

How Go in Practice, Second Edition covers it

How deep it goes
Implements Scanner-based line processing and per-record JSON parsing, while deferring further optimization and parallelization.
Examples
Uses structured.log records and a bufio.Scanner loop that unmarshals each scanned line.
What it assumes
Assumes basic Go file opening and JSON unmarshaling.
What you can do after
Apply via bufio.Scanner and bufio.ScanLines to parse records incrementally and handle malformed entries individually.
How it connects
Connects buffered I/O, streaming, JSON parsing, CSV, and concurrency for scalable file processing.

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.