WEBVTT 0 00:02.220 --> 00:03.660 Welcome back. 1 00:03.720 --> 00:09.380 In this lecture you're going to learn how to scan for input from the command-line you're going to build 2 00:09.390 --> 00:13.100 a program that counts the number of lines in the input. 3 00:13.110 --> 00:15.780 All right let's get started. 4 00:15.850 --> 00:21.910 First of all let's talk about what is scanning and how to scan for input. 5 00:21.910 --> 00:26.290 Simply put you can think of scanning as reading a stream of input data. 6 00:26.410 --> 00:30.880 Line by line into a byte buffer In Go 7 00:30.920 --> 00:34.040 You can use the buffer are your packages scanner type. 8 00:34.040 --> 00:40.550 To do that it scans an input stream such as a file line by line into a buffer. 9 00:40.550 --> 00:44.930 By the way the scanner can retrieve the input data from anywhere. 10 00:44.930 --> 00:51.170 For example you can receive the input stream data from the command line or a network source on the Internet 11 00:52.100 --> 00:59.110 or a file and so on by default the scanner stops scanning when it sees a new line character. 12 00:59.120 --> 01:03.710 This is its default behavior but you can set it to stop for anything. 13 01:03.710 --> 01:07.330 For now we're going to take a look at its default behavior. 14 01:07.370 --> 01:10.380 Let's say you have an input stream from a file like. 15 01:10.390 --> 01:15.090 So here is the first line from the file waiting to be scanned. 16 01:15.140 --> 01:19.400 You can call the scan method of the scanner to retrieve the first line. 17 01:19.400 --> 01:26.360 Like so the scanner puts the scanner data into an internal byte buffer when it puts the data into the 18 01:26.360 --> 01:26.980 buffer. 19 01:27.020 --> 01:32.960 You can retrieve it by calling the text method of the scanner like so it returns a string so you can 20 01:32.960 --> 01:34.820 work on it however you want. 21 01:34.820 --> 01:40.940 By the way the scanner buffers the data because it wants to read it only when the data is ready. 22 01:40.940 --> 01:43.180 This way it becomes more efficient. 23 01:43.220 --> 01:45.760 You had learned about using a buffer previously. 24 01:46.010 --> 01:48.630 It is the same thing here. 25 01:48.680 --> 01:48.970 OK. 26 01:48.980 --> 01:55.820 Now you need to call the scan and text methods repeatedly after it consumes all of the lines in the 27 01:55.820 --> 02:03.410 input stream like so. When there is an error or no more data to scan the scan method returns false 28 02:03.890 --> 02:05.800 so it can stop scanning for the input. 29 02:06.170 --> 02:10.040 So when there is more data it returns true instead. 30 02:10.310 --> 02:13.730 Okay now let's continue discussing the scanner in the coding. 31 02:13.730 --> 02:23.770 editor. You can create a scanner by using the NewScanner method of the bufio package like so. in means 32 02:23.860 --> 02:25.030 input stream. 33 02:25.330 --> 02:27.990 Okay let's take a look at its documentation. 34 02:28.060 --> 02:34.200 It accepts an IO.Reader interface and returns a pointer to a struct value. 35 02:34.210 --> 02:38.170 You don't have to know about interfaces and structs to use the scanner. 36 02:38.170 --> 02:44.900 For now let's pass it the standard in like so like everything in the Linux operating system standard. 37 02:44.900 --> 02:49.670 It is also a file and it supports the io.Reader interface. 38 02:49.690 --> 02:53.030 That's why I can pass it to the new scanner function here. 39 02:53.110 --> 02:55.240 You'll see how it works in a second. 40 02:55.270 --> 02:55.690 OK. 41 02:55.810 --> 03:01.220 Now the in variable contains a reference to a newly created scanner. 42 03:01.360 --> 03:04.780 Let's scan for the input stream by using the scan method. 43 03:04.780 --> 03:13.250 Like so when I run the program it waits for me to type something on the surface it looks like it doesn't 44 03:13.250 --> 03:14.070 do anything. 45 03:14.450 --> 03:16.480 However the scanner has buffered it (the data). 46 03:16.490 --> 03:20.060 The first line of the input to its own hidden buffer. 47 03:20.060 --> 03:21.040 Behind the scenes. 48 03:21.260 --> 03:25.030 So you need to get the data from it by using the text method. 49 03:25.040 --> 03:28.740 I'm going to call println "scanned text" in.Text. 50 03:28.790 --> 03:29.020 Okay. 51 03:29.030 --> 03:31.410 Let's take a look at its documentation. 52 03:31.430 --> 03:39.400 It just returns the scanned line as a string as you can see by using the text method you can retrieve 53 03:39.430 --> 03:46.720 the buffer data like so please note that it only puts the last scanned line into the buffer. 54 03:46.720 --> 03:50.260 So for example let me call the scan method a few times. 55 03:50.260 --> 03:58.960 Like so let me also print the buffered data a few times I have typed the lines and I pressed the Enter 56 03:58.960 --> 04:00.410 key three times. 57 04:00.610 --> 04:07.330 However in the end it only gets me the last line no matter how many times I call the text method. 58 04:07.330 --> 04:09.610 So let me take these back. 59 04:09.640 --> 04:15.610 I can also get the bytes of the input like so these may become handy when you are working with bytes 60 04:15.610 --> 04:15.970 only. 61 04:15.970 --> 04:20.310 For example I could have appended the returned bytes to a byte slice here 62 04:20.320 --> 04:23.300 if I wanted to. It print the bytes that I've typed. 63 04:23.600 --> 04:25.320 OK let me comment out it for now. 64 04:25.840 --> 04:29.950 Okay now let's say I want to read all the lines from the input data. 65 04:30.100 --> 04:33.520 To do that I'm going to use a loop like so I can do so. 66 04:33.550 --> 04:40.400 Because the scan method returns a bool value when the input data ends or there is an error. 67 04:40.420 --> 04:41.860 It returns false. 68 04:41.890 --> 04:49.700 So the loop eventually will quit as you can see whenever I hit the enter key it prints me 69 04:49.700 --> 04:53.120 what I've typed I am inside of the loop right now. 70 04:53.160 --> 04:56.550 So it keeps me asking for input to create from the loop. 71 04:56.550 --> 04:59.610 I can hit the Control+D keys together. 72 04:59.610 --> 05:04.380 If you are on Windows you may need to use Control+Z keys together. 73 05:04.380 --> 05:10.370 So now it quits from the loop because the scan method has returned false as the last example. 74 05:10.380 --> 05:12.530 Let's get the input from a file. 75 05:12.540 --> 05:15.120 I don't have to change my code to do that. 76 05:15.120 --> 05:22.200 I can simply redirect the standard in to the program like so cool I can read the file line by line. 77 05:22.200 --> 05:27.170 This works because the scanner doesn't care where it gets the input from here. 78 05:27.180 --> 05:29.740 It gets the input from the standard input. 79 05:29.940 --> 05:36.240 And by using this I redirected the standard input to the contents of the proverbs.txt file. 80 05:36.690 --> 05:42.090 This is called the command redirection and you can find more information about it in the resources of 81 05:42.090 --> 05:46.030 this lecture by the way you can find the proverbs file. 82 05:46.140 --> 05:52.640 Also in the resources of this lecture okay now I can count the lines in the file very simply I'm 83 05:52.640 --> 05:58.610 going to declare a variable here like so that I'm going to comment out the text call from here. 84 05:58.610 --> 06:05.270 You understand how it works. Instead, I'm going to increment the lines variable here because on each iteration 85 06:05.330 --> 06:07.760 the scanner returns a single line. 86 06:07.760 --> 06:13.830 Lastly I'm going to print the number of lines like so there are D lines in the file. 87 06:14.000 --> 06:18.000 I'm going to pass the lines variable here. Awesome! 88 06:18.170 --> 06:23.420 Congrats you have created a program that can count the lines from any input stream. 89 06:23.480 --> 06:25.210 There is one last thing though. 90 06:25.610 --> 06:28.430 What happens if there is an error when reading the file. 91 06:29.030 --> 06:35.900 While the scanner has an error method that returns an error instead of handling the error in the loop 92 06:35.960 --> 06:42.710 each time it allows you to handle it once it's because the scan method returns false when there is an 93 06:42.710 --> 06:43.490 error. 94 06:43.520 --> 06:48.090 Let's check for the error and print it. 95 06:48.130 --> 06:50.890 I'm going to close to standard input above like so. 96 06:51.730 --> 06:55.280 So it will return an error when I try to scan it. 97 06:55.570 --> 06:59.170 As you can see now it says that the file has already been closed. 98 06:59.680 --> 07:03.540 So now you have learned how to check for the errors with scanner as well. 99 07:04.250 --> 07:04.810 All right. 100 07:04.820 --> 07:05.700 That's all for now. 101 07:05.750 --> 07:07.060 See you in the next lecture bye!