How to Read File Line by Line in Golang?
Overview
Objects of type os represent files in Go.File is also called file handler. Package os provides functionality for file operations. Package bufio implements buffered I/O providing buffering and some functions for I/O.
How to Read File Line by Line in Golang?
The Scanner provides a convenient interface for reading data from files of newline delimited lines of text.
scanner := bufio.NewScanner(r Reader) :
- NewScanner returns a new Scanner to read from r
- Split function defaults to ScanLines
bool := scanner.Scan() :
- Scan advances the Scanner to the next token which will be available through Text()
- It returns false when the scan stops: either by reaching the end of input or an error
string := scanner.Text() :
- It returns the token generated by a call to Scan()
The following are the steps used for reading the text file line by line in the go language :
- Using the function os.open() for opening the text file
- Using the function bufio.NewScanner() for creating a scanner file
- Using the function bufio.ScanLines() along with the scanner for splitting the file into lines
- Lastly, using the function scanner which is Scan() in a for loop for getting each line and processing it
Reading File Line by Line to String Array
By using a standard method of using a function and printing the text, we can use an array for efficient code.
The problem with using the normal method is as follows :
- Reading from the command line argument since the text file path is hard coded.
- Processing the lines of file which in return keeps the resources on for a longer time period. We can read the data of the file in the string array and can then process it accordingly. This is however not recommended for files having large sizes.
- Below is the code for reading a file line by line to a string array.
Below is the code for reading a file line by line to a string array
Code :
The file is saved as main.go
Output :
Explanation :
In this example, instead of just printing Golang read file line by line at the output, we have stored it in an array and displayed that array at the output. Here, we have created an array of file lines and have appended the words one by one into the array using the append method. Once the words are appended in the array, it is printed by using the print function.
Examples for Understanding
Example - 1 :
Write a program to read a text file in the Go language :
Output :
Explanation :
In this example, the variable fn is initialized with a text file. Followed by this, the file is opened in read-only mode and once the file is done reading, it gets closed. The for loop is used for reading all the words in the file and then printing it.
Example - 2 :
Write a program in the Go language for reading a text file line by line :
Output :
Explanation :
This example is similar to that of the previous except that the function is used at a much later step and uses more conditional statements like if and for loop in the code for doing the same task which is printing every line of the text file.
Note :
the file data.txt or vivo.txt should be in the same directory as written in the code given else an error will be popped which prints - “open data.txt: no such file or directory”.
Conclusion
- Files in Go are represented by objects of type os. The file is also called a file handler. Package os provides functionality for file operations. Package bufio implements buffered I/O providing buffering and some functions for I/O.
- The Scanner provides a convenient interface for Golang read file line by line of newline delimited lines of text.
- scanner := bufio.NewScanner(r Reader)
- bool := scanner.Scan()
- string := scanner.Text()
- By using a standard method of using a function and printing the text, we can use an array for efficient code.