Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast file reading in golang

Tags:

file-io

go

I have a very large file and i need to process each line (each line of file is independent). How can I use goroutines (or should I not use them?) to read the file in the fastest way?

like image 212
Ondra Avatar asked Oct 16 '12 12:10

Ondra


People also ask

How to read a text file in Golang?

The simplest way of reading a text or binary file in Go is to use the ReadFile () function from the os package. This function reads the entire content of the file into a byte slice, so you should be careful when trying to read a large file - in this case, you should read the file line by line or in chunks.

How do I read the contents of a file in go?

Read the entire file in GoLang In Go, Reading an entire file content/text is to use ReadFile () function from the ioutil/os package. This function reads the entire content of the file into a byte slice. The ioutil package should not be used in reading a large file thus the function is quite sufficient for small files.

How to create a newscanner in Golang?

First, we need to create an instance with the NewScanner function which is a very familiar way for constructing the structs in golang. this function accepts a Reader interface as input, and the good news is os.File implemented this interface. It means, we can open a file and pass the pointer of the file to bufio.NewScanner. Let’s see it in action.

How to read data from a file using ioutil in go?

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. 1. Reading an entire file into memory To read an entire file into a variable, use the ReadFile function from the ioutil library.


1 Answers

As long as your hard disk is orders of magnitude slower than your CPU, which is still a quite common situation, then you cannot magically make the file reading (domain: from a single HD) any faster by throwing more CPU cycles onto it. (Assuming cold file caches and/or file size much bigger then all available file cache memory).

like image 180
zzzz Avatar answered Oct 10 '22 21:10

zzzz