Reading files in Go (read the file line by line)

R

Apart from dealing with bugs, file operations are probably one of the most common operations when it comes to programs.

In Go, you will find instances where you need to read the file line by line, especially when working with text files.

In this tutorial, we will tell you about all the methods and techniques on how we can read a file line by line.

We start with basic file reading techniques and move on to more advanced features like Buffio and the scanner library. We'll also give an example to help solidify your understanding.

sample file

Before we get into the steps of reading a file line-by-line, let's set up a basic file that we will use for demonstration purposes.

In our case, we use a basic Terraform configuration as shown in the following output:

# Define AWS provider configuration

the provider “Oh” ,

area= “us-ex-1”
,
# Create a VPC

resources “aws_vpc” “example_vpc” ,

cidr_block= “10.0.0.0/16”

enable_dns_support= Truth

enable_dns_hostnames= Truth
,

# Create two subnets in different availability zones
resources “aws_subnet” “example_subnet1” ,
vpc_id = aws_vpc.example_vpc.id
cidr_block= “10.0.1.0/24”
availability_zone= “us-ex-1a”
map_public_ip_on_launch= Truth
,
resources “aws_subnet” “example_subnet2” ,
vpc_id = aws_vpc.example_vpc.id
cidr_block= “10.0.2.0/24”
availability_zone= “us-ex-1b”
map_public_ip_on_launch= Truth
,
# Create a security group for the EC2 instance
resources “aws_security_group” “example_sg” ,
name = “example-sg”
Description = “Security Groups for EC2 Instances”
Entry ,
from_port= 22
to_port= 22
protocol = “tcp”
cidr_blocks= [“0.0.0.0/0”]
,
# —- More ———————

The example file represents a Terraform configuration for setting up EC2 with a load balancer.

open file in golang

Before we can read a file line by line, we need to open the file. Fortunately, Go provides us with a simple and intuitive way to open files using OS packages.

An example code is as follows:

package main
Import ,

“fmt”

“OS”
,

main work,, ,
FP := “./aws.tf”
, open file For Reading
fileerr := os.open,fp,
If to make a mistake ,= zero ,
fmt.println,“Mistake:”mistake,
return
,
Defer the file. Stop it,,
, The file is now open and ready For Reading
,

In the given example, we start by importing the packages we need. In our case, we only need the fmt and OS packages.

Next, we specify the path to the file we want to open and save it in the “fp” variable. In our case, we want to open the “aws.tf” file.

Finally, we use the os.Open() function to open the file and check for any errors that occur while trying to open the file.

Read file line by line in golang

Now that we have the target file open and ready to read, we can go ahead and discuss the methods and techniques of reading the file line by line.

Using Bufio Package

One of the most powerful packages in the Go ecosystem is the “bufio” package. This package implements buffered I/O operations. It wraps the “io.Reader” and “io.Writer” objects, creating another object that implements the interface but with buffer support.

Method 1: Using Bufio.Scanner

We can use the Scanner type from this package to read a file line by line as shown in the following example code:

package main
Import ,
“Bufio”
“fmt”
“OS”
,
main work,, ,
FP := “./aws.tf”
fileerr := os.open,fp,
If to make a mistake ,= zero ,
fmt.println,“Mistake:”mistake,
return
,
Defer the file. Stop it,,
Scanner := bufio.NewScanner,file,
For scanner.scan,, ,
row := scanner.text,,
fmt.println,Line,
,
If error := scanner.error,,, to make a mistake ,= zero ,
fmt.println,“Mistake:”mistake,
,
,

To use the Scanner type, we start by creating a Scanner using the bufio.NewScanner(file) method where file refers to the file we opened for reading.

We then use a “for” loop to iterate over the rows of the file using the scanner.scan() method.

Finally, we extract the text of each line using the scanner.text() method and print it to the console.

Method 2: Using Beaufio.newreader

Another method we can use is the newReader() method from the “bufio” package. It allows us to read lines of a file using the ReadString() function.

An example is as follows:

package main
Import ,
“Bufio”
“fmt”
“OS”
,
main work,, ,
FP := “./aws.tf”
fileerr := os.open,fp,
If to make a mistake ,= zero ,
fmt.println,“Mistake:”mistake,
return
,
Defer the file. Stop it,,
Reader := bufio.NewReader,file,
For ,
line, error := reader.readstring,'\n',
If to make a mistake ,= zero ,
to break
,
fmt.print,Line,
,
,

In this approach, we create a “bufio.Reader” using bufio.NewReader(file).

Then we use a “for” loop to read the rows using reader.readString('\n'). The loop continues reading the file until an error occurs. For example, when we reach the end of the file.

Using io package

Another package that deals with I/O operations in Go is the “io” package. This package provides basic interfaces to I/O primitives. The primary function of a package is to wrap primitives from existing implementations such as OS packages into shared public interfaces that abstract functionality.

Method 1: Using ReadString

In the “io” package, we can use the ReadString() function to read the file line by line as shown in the following example:

package main
Import ,
“fmt”
“io”
“OS”
,
main work,, ,
FP := “./aws.tf”
fileerr := os.open,fp,
If to make a mistake ,= zero ,
fmt.println,“Mistake:”mistake,
return
,
Defer the file. Stop it,,
buff := Make,[]byte, 1024,
For ,
n, err := file.read,buf,
If to make a mistake ,= zero ,
If error==io.EOF ,
to break
,
fmt.println,“Mistake:”mistake,
return
,
data := buf[:n]
fmt.print,string,data,,
,
,

In this implementation, we start by opening a file for reading as we did in the previous examples.

Then we create a buffer called “buff” to read a chunk of data from the file.

Finally, we use a “for” loop with the file.read() function to read the data into the buffer. We also check “io.EOF” to determine when we reached the end of the file and terminate the loop.

Method 2: Using Readline

We also have access to the “readlines” function from the “io” package which can come in handy when we need to read a file line by line.

An example is as follows:

package main
Import ,

“fmt”

“io”

“OS”
,

func splitlines,data []byte, [][]byte ,

there were lines [][]byte
For _, b := range data ,

If B== '\n' ,

rows = add,lines, []byte,,,

, Other ,

If lane,lines, , 0 ,

rows = add,lines, []byte,,,

,

lines[len(lines)1] = attach,lines[len(lines)1]b,

,

,

return lines
,

main work,, ,
FP := “./aws.tf”
fileerr := os.open,fp,
If to make a mistake ,= zero ,
fmt.println,“Mistake:”mistake,
return
,
Defer the file. Stop it,,
reader := io.reader,file,
buff := Make,[]byte, 1024,
For ,
n, error := reader.read,buf,
If to make a mistake ,= zero ,
If error==io.EOF ,
to break
,
fmt.println,“Mistake:”mistake,
return
,
data := buf[:n]
lines := splitlines,data,

For _, line := series lines ,
fmt.println,string,Line,,
,
,
,

In this case, we create a buffer and read chunks of data from the file using the reader.read() function.

We then define a splitLines() function whose purpose is to split the data into lines by detecting where a newline character is in the buff.

Finally, we print the contents of the file.

conclusion

In this tutorial, we learned a lot about file operations in Go. We learned how to use the “bufio” package, scanner types and NewReader. We also learned how to use methods like “readstring” and “readline” functions to read a file line by line.

Add comment

By Ranjan