Files
learngo/interfaces/log-parser/oop/textlog.go

40 lines
601 B
Go
Raw Normal View History

2019-08-17 15:55:25 +03:00
// For more tutorials: https://bp.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"bufio"
"io"
)
2019-08-26 14:37:58 +03:00
type textLog struct {
reader io.Reader
2019-08-17 15:55:25 +03:00
}
2019-08-26 14:37:58 +03:00
func newTextLog(r io.Reader) *textLog {
return &textLog{reader: r}
2019-08-17 15:55:25 +03:00
}
2019-08-26 14:37:58 +03:00
func (p *textLog) each(yield resultFn) error {
defer readClose(p.reader)
2019-08-17 15:55:25 +03:00
2019-08-26 21:52:47 +03:00
in := bufio.NewScanner(p.reader)
2019-08-17 15:55:25 +03:00
for in.Scan() {
2019-08-26 21:52:47 +03:00
r := new(result)
if err := r.UnmarshalText(in.Bytes()); err != nil {
return err
2019-08-17 15:55:25 +03:00
}
2019-08-26 21:52:47 +03:00
yield(*r)
2019-08-17 15:55:25 +03:00
}
return in.Err()
}