os.Open opens a file and returns a handle for controlled, sequential processing. Check the error, defer file.Close(), and use the handle for metadata inspection or incremental reads instead of loading the entire file into memory. section 7.1.1
Listing 7.4 Using bufio to read line by line
package main
import (
"bufio"
"encoding/json"
"log"
"os"
)
func main() {
file, err := os.Open("structured.log")
if err != nil {
panic(err)
}
defer file.Close()
scan := bufio.NewScanner(file) #1
scan.Split(bufio.ScanLines) #2
lineJSON := make(map[string]interface{})
for scan.Scan() { #3
if err := json.Unmarshal([]byte(scan.Text()), &lineJSON); err != n
il {
log.Println(err)
} else {
log.Println(lineJSON["level"])
}
}
}
Wrap the opened handle in bufio.Scanner to process newline-separated JSON records one line at a time. section 7.1.1

