📄 Go – Read File Line by Line with bufio.Scanner (2025 Guide)
🧲 Introduction – Why Read a File Line by Line?
Reading a file line by line is one of the most common file-processing tasks in Go. It’s ideal for log processing, configuration files, data ingestion, and streaming large text files without consuming too much memory.
🎯 In this guide, you’ll learn:
- How to read a file line by line in Go
- Use
bufio.ScannerwithScanLines - Handle errors and large files efficiently
- Best practices for scalable file parsing
✅ Example – Read File Line by Line
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func main() {
file, err := os.Open("sample.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
fmt.Println(line)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
📤 Output Example (for a file containing):
Line 1
Line 2
Line 3
Line 1
Line 2
Line 3
✅ This method uses bufio.Scanner with the default splitter (ScanLines) to read each line one by one.
⚙️ How It Works
scanner.Scan()reads the next line of inputscanner.Text()returns the current line as a string- Loop continues until EOF or error occurs
🔄 Customize Line Parsing Logic
lineNumber := 1
for scanner.Scan() {
fmt.Printf("Line %d: %s\n", lineNumber, scanner.Text())
lineNumber++
}
✅ Add line numbers, conditional parsing, or filtering inside the loop.
🧠 Best Practices
| Practice | Why It Helps |
|---|---|
✅ Use defer file.Close() | Ensures proper cleanup |
✅ Always check scanner.Err() | Detects read errors |
| ✅ Suitable for large files | Reads line-by-line without memory overhead |
❌ Avoid using ioutil.ReadAll() | Loads entire file—inefficient for big files |
🧰 Alternative – Use bufio.Reader.ReadString('\n')
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
fmt.Print(line)
}
✅ More control, but requires manual handling of end-of-file and partial lines.
📌 Summary – Recap & Next Steps
Go’s bufio.Scanner makes it easy to read files line by line, especially for log processors, large datasets, or configuration readers. It’s lightweight, safe, and idiomatic Go.
🔍 Key Takeaways:
- Use
bufio.Scannerto stream files line-by-line - Efficient for large files (no need to load into memory)
- Default split function is
ScanLines - Always check for scan errors and defer file close
⚙️ Next: Learn how to Read Word by Word, Write Lines to Files, or Count Lines in a File.
❓ FAQs – Go Read File Line by Line
❓ What package should I use to read a file line by line?
✅ Use bufio with os.Open() and bufio.NewScanner().
❓ How do I read a file efficiently in Go?
✅ Use streaming techniques like bufio.Scanner or bufio.Reader.
❓ What happens if the file has millions of lines?
✅ bufio.Scanner is designed to handle large files efficiently, reading line by line.
❓ Can I count lines while reading?
✅ Yes, increment a counter inside the scan loop.
❓ Is ioutil.ReadAll() better for small files?
✅ Yes, but avoid it for large files—prefer streaming with bufio.
Share Now :
