How to Read File Word By Word in Golang?


When working with files in Go, it is often necessary to read them word by word. In this article, we will explore how to read a file word by word in Golang. We will discuss three different approaches to accomplish this task and provide code snippets to demonstrate each method.

Approach 1: Using bufio.NewScanner

The first approach is to use the bufio package and its NewScanner function to read the file word by word. This function takes an io.Reader interface as input and returns a Scanner object that can be used to scan the file word by word.

Example

Here is an example code snippet that demonstrates this approach −

package main

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

func main() {
   file, err := os.Open("filename.txt")
   if err != nil {
      panic(err)
   }
   defer file.Close()
   
   scanner := bufio.NewScanner(file)
   
   // Set the split function for the scanning operation.
   scanner.Split(bufio.ScanWords)
   
   // Scan all words from the file.
   for scanner.Scan() {
      fmt.Println(scanner.Text())
   }
   
   if err := scanner.Err(); err != nil {
      panic(err)
   }
}

In this example, we first open the file using the os package and check for errors. Then we create a new scanner using the bufio package and set the split function to bufio.ScanWords to read the file word by word. Finally, we loop over each word and print it to the console.

Approach 2: Using fmt.Fscanf

The second approach is to use the fmt package and its Fscanf function to read the file word by word. This function takes an io.Reader interface as input and returns the number of items successfully scanned.

Example

Here is an example code snippet that demonstrates this approach −

package main

import (
   "fmt"
   "os"
)

func main() {
   file, err := os.Open("filename.txt")
   if err != nil {
      panic(err)
   }
   defer file.Close()
   
   var word string
   
   for {
      _, err := fmt.Fscanf(file, "%s", &word)
      if err != nil {
         break
      }
      fmt.Println(word)
   }
}

In this example, we first open the file using the os package and check for errors. Then we create a variable called word and loop over the file using fmt.Fscanf function until the end of the file is reached. The function scans the file for the next word and stores it in the word variable, which we then print to the console.

Approach 3: Using bufio.NewReader and strings.Fields

The third approach is to use the bufio package and its NewReader function along with the strings package and its Fields function to read the file word by word. The NewReader function creates a new buffered reader, and the Fields function splits the string into words.

Example

Here is an example code snippet that demonstrates this approach −

package main

import (
   "bufio"
   "fmt"
   "os"
   "strings"
)

func main() {
   file, err := os.Open("filename.txt")
   if err != nil {
      panic(err)
   }
   defer file.Close()
   
   reader := bufio.NewReader(file)
   
   for {
      line, err := reader.ReadString('\n')
      if err != nil {
         break
      }
      words := strings.Fields(line)
      for _, word := range words {
         fmt.Println(word)
      }
   }
}

In this example, we first open the file using the os package and check for errors. Then we create a new reader using the bufio package and loop over each line in the file using the reader.ReadString function. The function reads each line in the file until it reaches a newline character (\n) and returns the line as a string.

However, if we want to read the file word by word, we need to split each line into words. To do this, we can use the strings package in Go.

Example

Let's see an example of how to read a file word by word in Go −

package main

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

func main() {
   file, err := os.Open("test.txt")
   if err != nil {
      panic(err)
   }
   defer file.Close()
   
   scanner := bufio.NewScanner(file)
   scanner.Split(bufio.ScanWords)
   
   for scanner.Scan() {
      word := scanner.Text()
      fmt.Println(word)
   }
   
   if err := scanner.Err(); err != nil {
      panic(err)
   }
}

In the above example, we first open the file using the os package and check for errors. Then we create a new scanner using the bufio package and set its split function to bufio.ScanWords. This tells the scanner to split the input into words.

Next, we loop over each word in the file using the scanner.Scan() function, which reads the next word from the input and returns true if it was successful. We then get the word using the scanner.Text() function and print it to the console.

Finally, we check for any errors using the scanner.Err() function.

Conclusion

Reading a file word by word in Go is easy with the bufio package and the strings package. By using the bufio.ScanWords function, we can split the input into words and loop over each word in the file.

Updated on: 25-Apr-2023

328 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements