Trusted answers to developer questions

File reading in Golang

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

In Go, the ReadFile() function of the ioutil library is used to read data from a file. Using ioutil makes file reading easier as you don’t have​ to worry about closing files or using buffers.

svg viewer

1. Reading an entire file into memory

To read an entire file into a variable, use the ReadFile function from the ioutil library. This is the most basic type of file reading that can be required. The code below shows how to do this.

main.go
file.txt
package main
import (
"fmt"
"io/ioutil"
)
func main() {
data, err := ioutil.ReadFile("file.txt")
if err != nil {
fmt.Println("File reading error", err)
return
}
fmt.Println("Contents of file:")
fmt.Println(string(data))
}

The err variable gets an error description if an error occurs while reading the file. In the case of any problem, the error gets displayed to the user.

2. Reading file in bytes

Sometimes, it’s not feasible to read the whole file in one go, especially when the file size is too large. So, instead,​ we can read the file in chunks of set bytes. The following code reads the file in 3-byte chunks.

main.go
file.txt
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
)
func main() {
fptr := flag.String("fpath", "file.txt", "file path to read from")
flag.Parse()
f, err := os.Open(*fptr)
if err != nil {
log.Fatal(err)
}
defer func() {
if err = f.Close(); err != nil {
log.Fatal(err)
}
}()
r := bufio.NewReader(f)
b := make([]byte, 3)
for {
n, err := r.Read(b)
if err != nil {
fmt.Println("Error reading file:", err)
break
}
fmt.Println(string(b[0:n]))
}
}

Explanation

  • Line 14: opens the file using the path passed from the command line flag. The file is opened in variable f.
  • Line 18: makes a function that defers closing the file.
  • Line 23: initiates a new reader that reads the file.
  • Line 24: makes an array of 3-bytes. This array holds the bytes read from our file.
  • Line 25-32: repeatedly reads bytes from the file into the array (r) until the end-of-file is reached; then, the bytes are printed.

3. Reading a file line by line

We can also read each line in a file separately. This method reduces strain on the memory as the entire file will not be read i​n one go.

main.go
file.txt
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
)
func main() {
fptr := flag.String("fpath", "file.txt", "file path to read from")
flag.Parse()
f, err := os.Open(*fptr)
if err != nil {
log.Fatal(err)
}
defer func() {
if err = f.Close(); err != nil {
log.Fatal(err)
}
}()
s := bufio.NewScanner(f)
for s.Scan() {
fmt.Println(s.Text())
}
err = s.Err()
if err != nil {
log.Fatal(err)
}
}

Explanation

  • Line 14: opens the file using the path passed from the command line flag. The file is opened in variable f.
  • Line 23: creates a new scanner from the file.
  • Line 24-26: scans the file and reads it line by line.

RELATED TAGS

golang
go
read
file
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?