Developing the Initial Version of colStats

Powerful Command-Line Applications in Go — by Ricardo Gerardi (49 / 127)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Chapter 5 Improving the Performance of Your CLI Tools | TOC | Writing Tests for colStats 👉

Let’s build the colStats tool and make sure it works before looking for ways to optimize it. The program will receive two optional input parameters each with a default value:

  1. -col: The column on which to execute the operation. It defaults to 1.
  2. -op: The operation to execute on the selected column. Initially, this tool will support two operations: sum, which calculates the sum of all values in the column, and avg, which determines the average value of the column. You can add more operations later if you want.

In addition to the two optional flags, this tool accepts any number of file names to process. If the user provides more than one file name, the tool combines the results for the same column in all files.

Create the directory performance/colStats under your book project’s directory:

​ ​$ ​​mkdir​​ ​​-p​​ ​​$HOME/pragprog.com/rggo/performance/colStats​
​ ​$ ​​cd​​ ​​$HOME/pragprog.com/rggo/performance/colStats​

Then initialize the Go module for this project:

​ ​$ ​​go​​ ​​mod​​ ​​init​​ ​​pragprog.com/rggo/performance/colStats​
​ go: creating new go.mod: module pragprog.com/rggo/performance/colStats

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.