refactor: log parser map/slice
This commit is contained in:
@ -25,8 +25,8 @@ type domain struct {
|
|||||||
//
|
//
|
||||||
// the parser struct is carefully crafted to be usable using its zero values except the map field
|
// the parser struct is carefully crafted to be usable using its zero values except the map field
|
||||||
type parser struct {
|
type parser struct {
|
||||||
sum map[string]int // visits per unique domain
|
sum map[string]domain // visits per unique domain
|
||||||
domains []domain // unique domain names
|
domains []string // unique domain names
|
||||||
total int // total visits to all domains
|
total int // total visits to all domains
|
||||||
lines int // number of parsed lines (for the error messages)
|
lines int // number of parsed lines (for the error messages)
|
||||||
}
|
}
|
||||||
@ -35,7 +35,7 @@ func main() {
|
|||||||
in := bufio.NewScanner(os.Stdin)
|
in := bufio.NewScanner(os.Stdin)
|
||||||
|
|
||||||
p := parser{
|
p := parser{
|
||||||
sum: make(map[string]int),
|
sum: make(map[string]domain),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan the standard-in line by line
|
// Scan the standard-in line by line
|
||||||
@ -61,18 +61,19 @@ func main() {
|
|||||||
|
|
||||||
// Collect the unique domains
|
// Collect the unique domains
|
||||||
if _, ok := p.sum[d.name]; !ok {
|
if _, ok := p.sum[d.name]; !ok {
|
||||||
p.domains = append(p.domains, d)
|
p.domains = append(p.domains, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
p.sum[d.name] += d.visits
|
|
||||||
p.total += d.visits
|
p.total += d.visits
|
||||||
|
d.visits += p.sum[name].visits
|
||||||
|
p.sum[name] = d
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the visits per domain
|
// Print the visits per domain
|
||||||
for _, d := range p.domains {
|
for _, name := range p.domains {
|
||||||
vis := p.sum[d.name]
|
d := p.sum[name]
|
||||||
|
|
||||||
fmt.Printf("%-25s -> %d\n", d.name, vis)
|
fmt.Printf("%-25s -> %d\n", d.name, d.visits)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the total visits for all domains
|
// Print the total visits for all domains
|
||||||
|
Reference in New Issue
Block a user