Welcome back.
Let's say you have a log file that contains information about the Web site visits.
In this lecture you're going to learn how to analyze or in other words how to parse a log file and generate
a report first of all let me show you how the program is going to work.
This is the log file.
You can find it from the resources of this lecture.
As you can see there are two fields per line separated by a space character.
The first field is the domain name and the second field is the number of visits for that domain in the
log file.
The domains are duplicated.
So you need to aggregate them to print the total visits per domain.
Let me run the program by redirecting its standard input to the file like so as you can see it passes
the file and it prints the total visits per domain.
All right let's get started.
First of all let me create a new scanner that uses the standard input.
Let's scan the log file line by line by calling the scan method there are two fields per line.
So now I'm going to split them.
Remember the text method returns the current line and the fields function splits the line into the words.
Now the fields variable contains a string slice. Let me print them so you can see how it works.
I'm going to say.
Domain dash visits I'm going to give it fields 0 and fields 1 the program correctly parses the log
file to make the code easier to read.
I'm going to put the fields in variables first.
Let's declare a variable for the domain name.
Let's convert the visits field to an integer.
For now I've skipped the error handling.
I'll do that later.
You need to keep of the total total visits per domain.
So I'm going to create a map variable like so it will store the domain names and the total visits per
domain.
I also need to initialize the map okay.
Now it's time to calculate the total visits for the domains.
First I'm going to get the total visits from the map and I'm going to add the current visits to the
map.
So if the domain name doesn't exist the map will create a new key for the domain and it will set it to
the visits variable.
Otherwise it will only increase the total visits for the domain.
Okay.
Now it's time to print the map first.
Let's print the header for the domain names and total visits next.
Let's draw a horizontal line forty five
characters are fine.
Now I'm going to range over the map.
Like so
and I'm going to print the total visits per domain it works.
So it brings the total visits per domain.
However when I run it a few times as you can see it prints the domain names randomly.
As I explained in the last lecture before usually, you shouldn't loop over the maps.
It's a tell-tale sign of a design mistake.
So what should you do.
I'm going to go to the top of the file here.
I'm going to declare a slice to store the unique domain names instead of using the map for printing
the domain names.
I'm going to use this slice.
Okay let's go down here.
I'm going to check whether the domain is in the map or not.
Like so the domain doesn't exist in the map.
I'm going to add it to the slice.
This way the slice will only contain the unique domain names here.
Instead of looping over the map I'm going to loop over the domain slice instead I so for the total visits
I'm going to get it from the map instead like so
even though I run it several times it always prints the result in the same order.
Awesome. Let's also sort the domain names using the sort package like so cool.
Okay.
Let me also print the total visits for every domain.
To do that let's go to the top of the file here.
I'm going to declare a variable like so.
Now let's go down to the loop here.
I'm going to add visits and total visits OK.
Let's go down and let's printed here
also.
Now it's time to handle the errors.
Let's go to the beginning of the loop.
First I'm going to check whether the current line has two fields or not.
If not I'm going to say wrong inputs like so
OK let's test it.
To do that I'm going to duplicate the log file here I'm going to remove the second field here to check
whether I correctly handled the error or not like so. Let me say this file as log_err_missing.txt.
Let's try it with the incorrect log file first.
Cool the program detects the error okay.
Now let's try something else.
Let me duplicate the log file again.
Let me change the value of the visits to a string this time I'm going to save it as log_err_str
the program skips that line.
So it ignores the error.
This is not good.
Let's handle the error like so. If there is an error I'm going to say wrong input then I'm going to
terminate the program. Cool, it detects the error but not so fast.
Let's see what happens if there is a negative visits count.
Let me duplicate log file again.
Here I'm going to change this to a negative number.
This time I'm going to save it as log_err_negative. Visits to a domain should always be zero or
a positive number.
Right.
So let's see what happens.
It says minus forty four people have visited all the domains.
Clearly I should handle this error as well.
To do that I'm going to add another condition here like so call it works.
Let's make the error message more descriptive by adding the error line as well.
To do that let's go to the top here.
I'm going to declare a new variable lines.
Now let's go to the loop here.
I'm going to increment it.
OK let's add it to the error messages.
First I'm going to print the error line for the first message.
Like so
Cool, now it tells you that the error is in the third line.
So, why stop here?
Let's also add it to the second error message as well like so
excellent.
It works.
OK now let's handle the error from the scanner as well.
Let me also print it.
All right.
That's all for now in the upcoming sections you will revisit this project several times.
So, well done, congrats!
See you in the next section.