Go – File Handling
Estimated reading: 3 minutes 26 views

📂 Go – Read File Word by Word with bufio & Examples (2025 Guide)

🧲 Introduction – Why Read a File by Word?

Reading a file word by word is a common need in text analysis, tokenization, or building search engines. Go provides a powerful and memory-efficient way to achieve this using the bufio.Scanner with the ScanWords split function.

🎯 In this guide, you’ll learn:

  • How to read a file word-by-word using bufio.Scanner
  • Use the ScanWords function for tokenization
  • Handle file reading safely and efficiently
  • Real-world applications like word count and indexing

✅ Example – Read File Word by Word

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)
    scanner.Split(bufio.ScanWords) // Split by words

    for scanner.Scan() {
        word := scanner.Text()
        fmt.Println(word)
    }

    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }
}

📤 Output Example (for a file containing):

Go is awesome
Go  
is  
awesome

✅ This code:

  • Opens the file
  • Scans word by word using bufio.ScanWords
  • Prints each word to the terminal

🔄 How bufio.ScanWords Works

  • ScanWords splits input based on space, newline, or tab
  • It’s a built-in splitter for scanners in Go
  • Alternative: Use bufio.ScanLines for line-by-line reading

🧠 Best Practices

PracticeWhy It Helps
✅ Always check scanner.Err()Captures scanning errors
✅ Use defer file.Close()Ensures proper file closure
❌ Don’t load entire fileStreaming with scanner avoids memory issues
✅ Validate file existenceAvoids unexpected crashes

📌 Summary – Recap & Next Steps

Go makes reading files word-by-word easy and efficient with bufio.Scanner and ScanWords. It’s perfect for text processing, tokenization, or content analysis without overloading memory.

🔍 Key Takeaways:

  • Use bufio.NewScanner with ScanWords to read by word
  • Handle errors and always close files with defer
  • Efficient even for large files

⚙️ Next: Explore Counting Word Frequency, Building a Word Index, or Stop Word Removal in Go.


❓ FAQs – Go Read File by Word

❓ What is ScanWords in Go?
✅ It’s a split function for bufio.Scanner that separates text into words.

❓ How do I read a file without loading it entirely into memory?
✅ Use bufio.Scanner—it reads the file in chunks, not all at once.

❓ Can I customize how words are split?
✅ Yes. You can write a custom split function using scanner.Split().

❓ What happens if the file doesn’t exist?
os.Open() will return an error. Always check and handle it.

❓ Is this method suitable for large files?
✅ Yes. It streams the content efficiently using buffered reading.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Go Read File by Word

Or Copy Link

CONTENTS
Scroll to Top