Reading Files
Read manageable files with os.ReadFile or scan lines from os.Open; check scanner errors and account for its token limit.
Use os.ReadFile when a file has a known, manageable size. For line-oriented processing, bufio.Scanner reads incrementally without loading the whole file.
Open a file, close it after use, and check Scanner.Err after the scan loop because read errors also stop scanning.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
file, err := os.Open("input.txt")
if err != nil {
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
if err := scanner.Err(); err != nil {
panic(err)
}
}In production
os.ReadFile uses memory proportional to the file size. Scanner's default token buffer is 64 KiB, including any line delimiter; increase it with Scanner.Buffer before scanning or use bufio.Reader for longer lines. Always check the final read error, even when the loop ended normally.
Enjoyed this? Get more software craft delivered to your inbox.