Search⌘ K
AI Features

Developing the Initial Version of colStats

Explore how to implement the csv2float() function in Go, reading CSV data via the csv package and converting a specified column to float64. Understand error handling and how to prepare data for calculations in CLI tools.

Implementing the csv2float() function

The last function we’ll implement in this file is the function csv2float() to parse the contents of the CSV file into a slice of floating point numbers that we can use to perform the calculations.

This function accepts two input parameters:

  • An io.Reader interface representing the source of CSV data.
  • An int representing the column to extract data from.

It returns a slice of float64 numbers and a potential error:

Go (1.6.2)
func csv2float(r io.Reader, column int) ([]float64, error) {
Implementing the csv2float() function

This uses a similar pattern to what we’ve done previously; we’re providing the io.Reader interface as the input parameter for ...