Files
learngo/28-methods/xxx-log-parser-methods/packaged/summarize.go

49 lines
1.1 KiB
Go
Raw Normal View History

2019-04-26 10:11:17 +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/
//
package main
import (
"fmt"
"strings"
2019-05-07 20:19:11 +03:00
"github.com/inancgumus/learngo/28-methods/xxx-log-parser-methods/packaged/metrics"
2019-04-26 10:11:17 +03:00
)
// summarize prints the report and errors if any
func summarize(rep *metrics.Report, errs ...error) {
2019-04-26 21:32:20 +03:00
// TODO: make it strings.Builder
const format = "%-30s %10s %20s\n"
const formatValue = "%-30s %10d %20d\n"
fmt.Printf(format, "DOMAIN", "VISITS", "TIME SPENT")
fmt.Println(strings.Repeat("-", 65))
2019-04-26 10:11:17 +03:00
next, cur := rep.Iterator()
for next() {
rec := cur()
2019-04-26 21:32:20 +03:00
fmt.Printf(formatValue, rec.Domain, rec.Visits, rec.TimeSpent)
2019-04-26 10:11:17 +03:00
}
2019-04-26 21:32:20 +03:00
fmt.Printf("\n"+formatValue, "TOTAL",
rep.Total().Visits, rep.Total().TimeSpent,
)
2019-04-26 10:11:17 +03:00
// only handle the errors once
dumpErrs(errs...)
}
// this variadic func simplifies the multiple error handling
func dumpErrs(errs ...error) {
for _, err := range errs {
if err != nil {
fmt.Printf("> Err: %s\n", err)
}
}
}