Go - Read File Line by Line

1. Introduction

Reading a file line by line is a fundamental requirement in many programming scenarios, particularly when processing large files that do not fit into memory. Go provides an efficient way to handle such tasks using buffered I/O and the bufio package. This post explores how to read a file line by line in Go.

Definition

Reading a file line by line means processing the file's content one line at a time, which can be done in Go using a bufio.Scanner. This is especially useful for parsing large files, allowing the program to work with one chunk of data at a time and reduce memory consumption.

2. Program Steps

1. Open the file for reading.

2. Create a new bufio.Scanner from the file.

3. Iterate over the lines using the scanner.

4. Handle any errors that may occur.

5. Close the file after reading.

3. Code Program

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
)

func main() {
	// Step 1: Open the file
	fileName := "example.txt"
	file, err := os.Open(fileName)
	if err != nil {
		log.Fatalf("failed to open file: %s", err)
	}
	defer file.Close() // Ensure the file is closed after reading

	// Step 2: Create a bufio.Scanner
	scanner := bufio.NewScanner(file)

	// Step 3: Iterate over the lines using the scanner
	fmt.Println("Reading file line by line:")
	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}

	// Step 4: Handle any possible scanning errors
	if err := scanner.Err(); err != nil {
		log.Fatalf("error reading file: %s", err)
	}
}

Output:

Reading file line by line:
First line of the example.txt file.
Second line of the same file.
And here's the third line.

Explanation:

1. package main - The package declaration for the Go program.

2. import statements - Bringing in bufio for buffered I/O, fmt for printing, and log for error logging.

3. os.Open is called to open the file with the provided fileName.

4. defer file.Close() defers the execution of file.Close() until the end of the main function.

5. bufio.NewScanner creates a new scanner that reads from the file.

6. scanner.Scan() reads the next line from the file, returning false when no more lines are available or an error occurs.

7. scanner.Text() returns the current line as a string, which is then printed out.

8. After the loop, scanner.Err() checks for any errors during scanning, excluding io.EOF which is expected when the end of the file is reached.

9. The output shows each line read from the file printed to the console.


Comments