Welcome back.
In this lecture you're going to learn how to scan for input from the command-line you're going to build
a program that counts the number of lines in the input.
All right let's get started.
First of all let's talk about what is scanning and how to scan for input.
Simply put you can think of scanning as reading a stream of input data.
Line by line into a byte buffer In Go
You can use the buffer are your packages scanner type.
To do that it scans an input stream such as a file line by line into a buffer.
By the way the scanner can retrieve the input data from anywhere.
For example you can receive the input stream data from the command line or a network source on the Internet
or a file and so on by default the scanner stops scanning when it sees a new line character.
This is its default behavior but you can set it to stop for anything.
For now we're going to take a look at its default behavior.
Let's say you have an input stream from a file like.
So here is the first line from the file waiting to be scanned.
You can call the scan method of the scanner to retrieve the first line.
Like so the scanner puts the scanner data into an internal byte buffer when it puts the data into the
buffer.
You can retrieve it by calling the text method of the scanner like so it returns a string so you can
work on it however you want.
By the way the scanner buffers the data because it wants to read it only when the data is ready.
This way it becomes more efficient.
You had learned about using a buffer previously.
It is the same thing here.
OK.
Now you need to call the scan and text methods repeatedly after it consumes all of the lines in the
input stream like so. When there is an error or no more data to scan the scan method returns false
so it can stop scanning for the input.
So when there is more data it returns true instead.
Okay now let's continue discussing the scanner in the coding.
editor. You can create a scanner by using the NewScanner method of the bufio package like so. in means
input stream.
Okay let's take a look at its documentation.
It accepts an IO.Reader interface and returns a pointer to a struct value.
You don't have to know about interfaces and structs to use the scanner.
For now let's pass it the standard in like so like everything in the Linux operating system standard.
It is also a file and it supports the io.Reader interface.
That's why I can pass it to the new scanner function here.
You'll see how it works in a second.
OK.
Now the in variable contains a reference to a newly created scanner.
Let's scan for the input stream by using the scan method.
Like so when I run the program it waits for me to type something on the surface it looks like it doesn't
do anything.
However the scanner has buffered it (the data).
The first line of the input to its own hidden buffer.
Behind the scenes.
So you need to get the data from it by using the text method.
I'm going to call println "scanned text" in.Text.
Okay.
Let's take a look at its documentation.
It just returns the scanned line as a string as you can see by using the text method you can retrieve
the buffer data like so please note that it only puts the last scanned line into the buffer.
So for example let me call the scan method a few times.
Like so let me also print the buffered data a few times I have typed the lines and I pressed the Enter
key three times.
However in the end it only gets me the last line no matter how many times I call the text method.
So let me take these back.
I can also get the bytes of the input like so these may become handy when you are working with bytes
only.
For example I could have appended the returned bytes to a byte slice here
if I wanted to. It print the bytes that I've typed.
OK let me comment out it for now.
Okay now let's say I want to read all the lines from the input data.
To do that I'm going to use a loop like so I can do so.
Because the scan method returns a bool value when the input data ends or there is an error.
It returns false.
So the loop eventually will quit as you can see whenever I hit the enter key it prints me
what I've typed I am inside of the loop right now.
So it keeps me asking for input to create from the loop.
I can hit the Control+D keys together.
If you are on Windows you may need to use Control+Z keys together.
So now it quits from the loop because the scan method has returned false as the last example.
Let's get the input from a file.
I don't have to change my code to do that.
I can simply redirect the standard in to the program like so cool I can read the file line by line.
This works because the scanner doesn't care where it gets the input from here.
It gets the input from the standard input.
And by using this I redirected the standard input to the contents of the proverbs.txt file.
This is called the command redirection and you can find more information about it in the resources of
this lecture by the way you can find the proverbs file.
Also in the resources of this lecture okay now I can count the lines in the file very simply I'm
going to declare a variable here like so that I'm going to comment out the text call from here.
You understand how it works. Instead, I'm going to increment the lines variable here because on each iteration
the scanner returns a single line.
Lastly I'm going to print the number of lines like so there are D lines in the file.
I'm going to pass the lines variable here. Awesome!
Congrats you have created a program that can count the lines from any input stream.
There is one last thing though.
What happens if there is an error when reading the file.
While the scanner has an error method that returns an error instead of handling the error in the loop
each time it allows you to handle it once it's because the scan method returns false when there is an
error.
Let's check for the error and print it.
I'm going to close to standard input above like so.
So it will return an error when I try to scan it.
As you can see now it says that the file has already been closed.
So now you have learned how to check for the errors with scanner as well.
All right.
That's all for now.
See you in the next lecture bye!