Files
learngo/logparser/oop/logcount.go

34 lines
604 B
Go
Raw Normal View History

2019-08-26 21:52:47 +03:00
// 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/
//
2019-08-28 20:23:38 +03:00
package main
2019-08-26 21:52:47 +03:00
import "fmt"
2019-08-28 20:23:38 +03:00
// logCount counts the yielded records
2019-08-26 21:52:47 +03:00
type logCount struct {
2019-08-28 20:23:38 +03:00
iterator
2019-08-26 21:52:47 +03:00
n int
}
2019-08-28 20:23:38 +03:00
func (lc *logCount) each(yield recordFn) error {
err := lc.iterator.each(func(r record) {
2019-08-26 21:52:47 +03:00
lc.n++
yield(r)
})
if err != nil {
// lc.n+1: iterator.each won't call yield on err
return fmt.Errorf("record %d: %v", lc.n+1, err)
}
return nil
}
func (lc *logCount) count() int {
return lc.n
}