From fba3125c8e4e8e20ea19e1948ae7dcaee9fe3f5e Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Fri, 26 Apr 2019 10:10:52 +0300 Subject: [PATCH] refactor: renamings map and struct log parser --- .../02-project-log-parser/main.go | 4 +-- .../05-project-log-parser-structs/main.go | 28 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/23-project-log-parser/02-project-log-parser/main.go b/23-project-log-parser/02-project-log-parser/main.go index babfcdb..5ffc2d4 100644 --- a/23-project-log-parser/02-project-log-parser/main.go +++ b/23-project-log-parser/02-project-log-parser/main.go @@ -38,6 +38,8 @@ func main() { return } + domain := fields[0] + // Sum the total visits per domain visits, err := strconv.Atoi(fields[1]) if visits < 0 || err != nil { @@ -45,8 +47,6 @@ func main() { return } - domain := fields[0] - // Collect the unique domains if _, ok := sum[domain]; !ok { domains = append(domains, domain) diff --git a/24-structs/05-project-log-parser-structs/main.go b/24-structs/05-project-log-parser-structs/main.go index be5b8d5..cd3394f 100644 --- a/24-structs/05-project-log-parser-structs/main.go +++ b/24-structs/05-project-log-parser-structs/main.go @@ -16,23 +16,23 @@ import ( "strings" ) -// visit stores metrics for a domain -type visit struct { +// result stores the parsed result for a domain +type result struct { domain string - total int + visits int // add more metrics if needed } // parser keep tracks of the parsing type parser struct { - sum map[string]visit // metrics per domain - domains []string // unique domain names - total int // total visits for all domains - lines int // number of parsed lines (for the error messages) + sum map[string]result // metrics per domain + domains []string // unique domain names + total int // total visits for all domains + lines int // number of parsed lines (for the error messages) } func main() { - p := parser{sum: make(map[string]visit)} + p := parser{sum: make(map[string]result)} // Scan the standard-in line by line in := bufio.NewScanner(os.Stdin) @@ -46,6 +46,8 @@ func main() { return } + domain := fields[0] + // Sum the total visits per domain visits, err := strconv.Atoi(fields[1]) if visits < 0 || err != nil { @@ -53,8 +55,6 @@ func main() { return } - domain := fields[0] - // Collect the unique domains if _, ok := p.sum[domain]; !ok { p.domains = append(p.domains, domain) @@ -67,9 +67,9 @@ func main() { // p.sum[name].count += visits // create and assign a new copy of `visit` - p.sum[domain] = visit{ + p.sum[domain] = result{ 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)) for _, domain := range p.domains { - visits := p.sum[domain] - fmt.Printf("%-30s %10d\n", domain, visits.total) + parsed := p.sum[domain] + fmt.Printf("%-30s %10d\n", domain, parsed.visits) } // Print the total visits for all domains