Files
learngo/27-functional-programming/log-parser-exp/textreader.go
2019-08-19 10:21:17 +03:00

39 lines
716 B
Go

// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"bufio"
"fmt"
"io"
"strings"
)
func textReader(r io.Reader) inputFn {
return func() ([]result, error) {
return parseText(bufio.NewScanner(r))
}
}
// TODO: custom error type for line information
func parseText(in *bufio.Scanner) ([]result, error) {
var res []result
for l := 1; in.Scan(); l++ {
fields := strings.Fields(in.Text())
r, err := parseFields(fields)
if err != nil {
return nil, fmt.Errorf("line %d: %v", l, err)
}
res = append(res, r)
}
return res, in.Err()
}