39 lines
825 B
Go
Raw Normal View History

2019-04-26 07:11:36 +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"
)
// summarize prints the report and errors if any
2019-04-26 10:28:06 +03:00
func summarize(rep *report, errs ...error) {
2019-04-26 07:11:36 +03:00
fmt.Printf("%-30s %10s\n", "DOMAIN", "VISITS")
fmt.Println(strings.Repeat("-", 45))
2019-04-26 10:28:06 +03:00
next, cur := rep.iterator()
2019-04-26 10:11:52 +03:00
for next() {
rec := cur()
fmt.Printf("%-30s %10d\n", rec.domain, rec.visits)
2019-04-26 07:11:36 +03:00
}
2019-04-26 10:28:06 +03:00
fmt.Printf("\n%-30s %10d\n", "TOTAL", rep.total.visits)
2019-04-26 07:11:36 +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)
}
}
}