Go - Read File Word By Word



When working with files in Go, it is often necessary to read them word by word. In this chapter, we will explore how to read a file word by word in Golang. We will discuss four 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

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.

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)
   }
}

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

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.

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)
   }
}

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

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.

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)
      }
   }
}

Example

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.

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)
   }
}

Approach 4: Using ioutil.ReadFile and strings.Fields

The fourth approach is to use the io/ioutil package and its ReadFile function to read the entire file into memory and then use the strings.Fields function to split the contents into words.

Example

In this example, we first read the entire file using the ioutil.ReadFile function and check for errors. This function reads the file's content into a byte slice, which we then convert to a string. Next, we use the strings.Fields function to split the content into words and loop over each word to print it to the console.

 package main
import (
   "fmt"
   "io/ioutil"
   "strings"
)

func main() {
   data, err := ioutil.ReadFile("filename.txt")
   if err != nil {
      panic(err)
   }
   
   content := string(data)
   words := strings.Fields(content)
   
   for _, word := range words {
      fmt.Println(word)
   }
}

Output

The Output of all the above program, if the file exists −

Hello
World
This
is
a
test
file

The Output, If the file doesn't exists

panic: open filename.txt: no such file or directory

Conclusion

Reading a file word by word in Go can be achieved using various approaches. By using the bufio package and its ScanWords function, we can split the input into words and loop over each word in the file. Additionally, the fmt.Fscanf function and the io/ioutil package offer alternative methods to accomplish this task. Each method has its advantages, and you can choose the one that best fits your needs.

Advertisements