refactor: renamings map and struct log parser

This commit is contained in:
Inanc Gumus
2019-04-26 10:10:52 +03:00
parent 8053066a6b
commit fba3125c8e
2 changed files with 16 additions and 16 deletions

View File

@ -38,6 +38,8 @@ func main() {
return return
} }
domain := fields[0]
// Sum the total visits per domain // Sum the total visits per domain
visits, err := strconv.Atoi(fields[1]) visits, err := strconv.Atoi(fields[1])
if visits < 0 || err != nil { if visits < 0 || err != nil {
@ -45,8 +47,6 @@ func main() {
return return
} }
domain := fields[0]
// Collect the unique domains // Collect the unique domains
if _, ok := sum[domain]; !ok { if _, ok := sum[domain]; !ok {
domains = append(domains, domain) domains = append(domains, domain)

View File

@ -16,23 +16,23 @@ import (
"strings" "strings"
) )
// visit stores metrics for a domain // result stores the parsed result for a domain
type visit struct { type result struct {
domain string domain string
total int visits int
// add more metrics if needed // add more metrics if needed
} }
// parser keep tracks of the parsing // parser keep tracks of the parsing
type parser struct { type parser struct {
sum map[string]visit // metrics per domain sum map[string]result // metrics per domain
domains []string // unique domain names domains []string // unique domain names
total int // total visits for all domains total int // total visits for all domains
lines int // number of parsed lines (for the error messages) lines int // number of parsed lines (for the error messages)
} }
func main() { func main() {
p := parser{sum: make(map[string]visit)} p := parser{sum: make(map[string]result)}
// Scan the standard-in line by line // Scan the standard-in line by line
in := bufio.NewScanner(os.Stdin) in := bufio.NewScanner(os.Stdin)
@ -46,6 +46,8 @@ func main() {
return return
} }
domain := fields[0]
// Sum the total visits per domain // Sum the total visits per domain
visits, err := strconv.Atoi(fields[1]) visits, err := strconv.Atoi(fields[1])
if visits < 0 || err != nil { if visits < 0 || err != nil {
@ -53,8 +55,6 @@ func main() {
return return
} }
domain := fields[0]
// Collect the unique domains // Collect the unique domains
if _, ok := p.sum[domain]; !ok { if _, ok := p.sum[domain]; !ok {
p.domains = append(p.domains, domain) p.domains = append(p.domains, domain)
@ -67,9 +67,9 @@ func main() {
// p.sum[name].count += visits // p.sum[name].count += visits
// create and assign a new copy of `visit` // create and assign a new copy of `visit`
p.sum[domain] = visit{ p.sum[domain] = result{
domain: domain, domain: domain,
total: visits + p.sum[domain].total, visits: visits + p.sum[domain].visits,
} }
} }
@ -80,8 +80,8 @@ func main() {
fmt.Println(strings.Repeat("-", 45)) fmt.Println(strings.Repeat("-", 45))
for _, domain := range p.domains { for _, domain := range p.domains {
visits := p.sum[domain] parsed := p.sum[domain]
fmt.Printf("%-30s %10d\n", domain, visits.total) fmt.Printf("%-30s %10d\n", domain, parsed.visits)
} }
// Print the total visits for all domains // Print the total visits for all domains