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
