interfaces: refactor

This commit is contained in:
Inanc Gumus
2019-08-19 10:21:11 +03:00
parent b95be49711
commit 158f475a2d
89 changed files with 784 additions and 2 deletions

View File

@ -0,0 +1,56 @@
// 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"
"os"
"text/tabwriter"
)
// TODO: make this configurable? or exercise?
const (
minWidth = 0
tabWidth = 4
padding = 4
flags = 0
)
type summarizeFunc func(iterator) error
func (f summarizeFunc) summarize(results iterator) error {
return f(results)
}
// type textSummary struct{}
// func newTextSummary() *textSummary {
// return new(textSummary)
// }
// func (s *textSummary) summarize(results iterator) error {
func textSummary(results iterator) error {
w := tabwriter.NewWriter(os.Stdout, minWidth, tabWidth, padding, ' ', flags)
write := fmt.Fprintf
write(w, "DOMAINS\tPAGES\tVISITS\tUNIQUES\n")
write(w, "-------\t-----\t------\t-------\n")
var total result
results.each(func(r result) {
total = total.add(r)
write(w, "%s\t%s\t%d\t%d\n", r.domain, r.page, r.visits, r.uniques)
})
write(w, "\t\t\t\n")
write(w, "%s\t%s\t%d\t%d\n", "TOTAL", "", total.visits, total.uniques)
return w.Flush()
}