Improving the colStats Tool to Process Files Concurrently

Learn how to improve the colStats tool to process files.

As we noticed from the tracer output, the tool is processing files sequentially. This isn’t efficient since several files have to be processed. By changing the program to process files concurrently, we can benefit from multiprocessor machines and use more CPUs, generally making the program run faster. The program will spend less time waiting for resources and more time processing files.

Add the sync package

One of the main benefits of Go is its concurrency model. Go includes concurrency primitives that allow us to add concurrency to our programs in a more intuitive way. By using goroutines and channels, we can modify the current colStats tool to process several files concurrently by making changes just to the run() function. The other functions remain unchanged.

First, we add the sync package to the imports section, which provides synchronization types such as the WaitGroup:

Get hands-on with 1200+ tech skills courses.