November 2018
Beginner to intermediate
348 pages
8h 32m
English
Parsing the data is a piece of cake with only the standard library:
func parse(l loader) (dates []string, co2s []float64) { s := bufio.NewScanner(l()) for s.Scan() { row := s.Text() if strings.HasPrefix(row, "#") { continue } fields := strings.Fields(row) dates = append(dates, fields[2]) co2, err := strconv.ParseFloat(fields[4], 64) dieIfErr(err) co2s = append(co2s, co2) } return}
The parsing function takes a loader, which when called, returns a io.Reader. We then wrap the io.Reader in a bufio.Scanner. Recall that the format is not standard. There are some things that we want and some things we don't. The data however is in a fairly consistent format—we can use the standard library functions to filter the ones we ...
Read now
Unlock full access