Final Touches to the Initial Version of colStats
Learn how to implement the colStats tool without optimization.
We'll cover the following...
Creating the main.go
file
We create the file main.go
to define the main()
function. We open
the file and add the package
and import
sections.
We’ll need the following packages:
flag
: To parse command-line options.fmt
: To print formatted output and create new errors.io
: So we can use theio.Writer
interface.os
: To interact with the operating system.
package mainimport ("flag""fmt""io""os")
Adding the package and import section
Creating the main()
function
Next, we create the function main()
to parse the command-line arguments and
call the function run()
, which is responsible for the main logic of the tool.
func main() {// Verify and parse argumentsop := flag.String("op", "sum", "Operation to be executed")column := flag.Int("col", 1, "CSV column on which to execute operation")flag.Parse()if err := run(flag.Args(), *op, *column, os.Stdout); err != nil {fmt.Fprintln(os.Stderr, err)os.Exit(1)}}
Adding the package and import section
If the run()
function returns any errors, we print them out to ...