| 
									
										
										
										
											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 ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // result stores the parsed result for a domain | 
					
						
							|  |  |  | type result struct { | 
					
						
							|  |  |  | 	domain string | 
					
						
							|  |  |  | 	visits int | 
					
						
							|  |  |  | 	// add more metrics if needed | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // parser keep tracks of the parsing | 
					
						
							|  |  |  | type parser struct { | 
					
						
							|  |  |  | 	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) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // newParser constructs, initializes and returns a new parser | 
					
						
							|  |  |  | func newParser() parser { | 
					
						
							|  |  |  | 	return parser{sum: make(map[string]result)} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // parse parses a log line and returns the parsed result with an error | 
					
						
							|  |  |  | func parse(p parser, line string) (parsed result, err error) { | 
					
						
							|  |  |  | 	fields := strings.Fields(line) | 
					
						
							|  |  |  | 	if len(fields) != 2 { | 
					
						
							|  |  |  | 		err = fmt.Errorf("wrong input: %v (line #%d)", fields, p.lines) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	parsed.domain = fields[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	parsed.visits, err = strconv.Atoi(fields[1]) | 
					
						
							|  |  |  | 	if parsed.visits < 0 || err != nil { | 
					
						
							|  |  |  | 		err = fmt.Errorf("wrong input: %q (line #%d)", fields[1], p.lines) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } |