| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | // Copyright © 2018 Inanc Gumus | 
					
						
							|  |  |  | // Learn Go Programming Course | 
					
						
							|  |  |  | // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ | 
					
						
							|  |  |  | // | 
					
						
							| 
									
										
										
										
											2019-10-30 19:34:44 +03:00
										 |  |  | // For more tutorials  : https://learngoprogramming.com | 
					
						
							|  |  |  | // In-person training  : https://www.linkedin.com/in/inancgumus/ | 
					
						
							|  |  |  | // Follow me on twitter: https://twitter.com/inancgumus | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"bufio" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"sort" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func main() { | 
					
						
							|  |  |  | 	p := newParser() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Scan the standard-in line by line | 
					
						
							|  |  |  | 	in := bufio.NewScanner(os.Stdin) | 
					
						
							|  |  |  | 	for in.Scan() { | 
					
						
							|  |  |  | 		p.lines++ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		parsed, err := parse(p, in.Text()) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			fmt.Println(err) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		domain, visits := parsed.domain, parsed.visits | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Collect the unique domains | 
					
						
							|  |  |  | 		if _, ok := p.sum[domain]; !ok { | 
					
						
							|  |  |  | 			p.domains = append(p.domains, domain) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Keep track of total and per domain visits | 
					
						
							|  |  |  | 		p.total += visits | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// create and assign a new copy of `visit` | 
					
						
							|  |  |  | 		p.sum[domain] = result{ | 
					
						
							|  |  |  | 			domain: domain, | 
					
						
							|  |  |  | 			visits: visits + p.sum[domain].visits, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Print the visits per domain | 
					
						
							|  |  |  | 	sort.Strings(p.domains) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Printf("%-30s %10s\n", "DOMAIN", "VISITS") | 
					
						
							|  |  |  | 	fmt.Println(strings.Repeat("-", 45)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, domain := range p.domains { | 
					
						
							|  |  |  | 		parsed := p.sum[domain] | 
					
						
							|  |  |  | 		fmt.Printf("%-30s %10d\n", domain, parsed.visits) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Print the total visits for all domains | 
					
						
							|  |  |  | 	fmt.Printf("\n%-30s %10d\n", "TOTAL", p.total) | 
					
						
							| 
									
										
										
										
											2019-04-27 10:45:47 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Let's handle the error | 
					
						
							|  |  |  | 	if err := in.Err(); err != nil { | 
					
						
							|  |  |  | 		fmt.Println("> Err:", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-26 21:32:20 +03:00
										 |  |  | } |