update: log parser dir to input scanning
This commit is contained in:
6
23-input-scanning/03-project-log-parser/log.txt
Normal file
6
23-input-scanning/03-project-log-parser/log.txt
Normal file
@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org 4
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org -100
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
6
23-input-scanning/03-project-log-parser/log_err_str.txt
Normal file
6
23-input-scanning/03-project-log-parser/log_err_str.txt
Normal file
@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org FOUR
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
78
23-input-scanning/03-project-log-parser/main.go
Normal file
78
23-input-scanning/03-project-log-parser/main.go
Normal file
@ -0,0 +1,78 @@
|
||||
// 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 (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
sum map[string]int // total visits per domain
|
||||
domains []string // unique domain names
|
||||
total int // total visits to all domains
|
||||
lines int // number of parsed lines (for the error messages)
|
||||
)
|
||||
|
||||
sum = make(map[string]int)
|
||||
|
||||
// Scan the standard-in line by line
|
||||
in := bufio.NewScanner(os.Stdin)
|
||||
for in.Scan() {
|
||||
lines++
|
||||
|
||||
// Parse the fields
|
||||
fields := strings.Fields(in.Text())
|
||||
if len(fields) != 2 {
|
||||
fmt.Printf("wrong input: %v (line #%d)\n", fields, lines)
|
||||
return
|
||||
}
|
||||
|
||||
domain := fields[0]
|
||||
|
||||
// Sum the total visits per domain
|
||||
visits, err := strconv.Atoi(fields[1])
|
||||
if visits < 0 || err != nil {
|
||||
fmt.Printf("wrong input: %q (line #%d)\n", fields[1], lines)
|
||||
return
|
||||
}
|
||||
|
||||
// Collect the unique domains
|
||||
if _, ok := sum[domain]; !ok {
|
||||
domains = append(domains, domain)
|
||||
}
|
||||
|
||||
// Keep track of total and per domain visits
|
||||
total += visits
|
||||
sum[domain] += visits
|
||||
}
|
||||
|
||||
// Print the visits per domain
|
||||
sort.Strings(domains)
|
||||
|
||||
fmt.Printf("%-30s %10s\n", "DOMAIN", "VISITS")
|
||||
fmt.Println(strings.Repeat("-", 45))
|
||||
|
||||
for _, domain := range domains {
|
||||
visits := sum[domain]
|
||||
fmt.Printf("%-30s %10d\n", domain, visits)
|
||||
}
|
||||
|
||||
// Print the total visits for all domains
|
||||
fmt.Printf("\n%-30s %10d\n", "TOTAL", total)
|
||||
|
||||
// Let's handle the error
|
||||
if err := in.Err(); err != nil {
|
||||
fmt.Println("> Err:", err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user