WEBVTT 0 00:01.270 --> 00:02.200 Welcome back! 1 00:02.230 --> 00:06.600 In this lecture, I'm going to write the name of the empty files to another file. 2 00:06.600 --> 00:07.750 All right! Let's get started. 3 00:09.220 --> 00:14.490 To save the names of the empty files to a file, I need another function from the Go standard 4 00:14.490 --> 00:15.720 library. 5 00:15.720 --> 00:17.360 It's in the ioutil package. 6 00:17.370 --> 00:23.480 Let me show you. As I said in the previous lecture, ioutil package has a lot of practical functions 7 00:23.480 --> 00:25.340 for working with files. 8 00:25.580 --> 00:31.680 For now, I'm only interested in the WriteFile function. WriteFile function 9 00:31.700 --> 00:35.620 basically writes what's inside the given byte slice to a file. 10 00:35.630 --> 00:41.020 It's that simple. So let's create a slice to store all the file names. 11 00:41.020 --> 00:45.640 But, I don't know how many bytes that I'm going to write to the file. So, I'm going to create a nil 12 00:45.640 --> 00:47.950 slice, like this one. 13 00:48.010 --> 00:54.660 Now I'm going to append the file name to the names slice like so. However, there is an error. 14 00:54.820 --> 01:00.230 It says that the name is a string but the names slice's element type is a byte. 15 01:00.430 --> 01:03.480 It is because the names slice is a byte slice. 16 01:03.610 --> 01:10.820 Remember you can only append a value to a slice with an identical type of that slice's element type. 17 01:10.830 --> 01:11.870 OK? 18 01:12.080 --> 01:16.610 So how can I convert the name string to byte values. 19 01:16.630 --> 01:22.730 Well, it's like this, I'll talk about this later in detail but for now let tell you that a string 20 01:22.730 --> 01:26.880 value is actually a bytes slice behind the scenes. Here, 21 01:26.990 --> 01:31.010 the name string value gets converted to a byte slice on the fly. 22 01:31.370 --> 01:37.700 Then I'm using the ellipses operator to append the elements in that bytes slice to the names slice. 23 01:37.790 --> 01:42.280 It's like appending the individual byte values manually. 24 01:42.290 --> 01:43.500 All right, let me print it 25 01:43.530 --> 01:52.980 using the s verb. This s verb converts the given bytes slice to a string value automatically. OK, let's run 26 01:52.980 --> 02:01.230 it. Oops! It doesn't print new lines after the file names. But, why?! Please pause the video and try to solve 27 02:01.230 --> 02:02.310 this problem. 28 02:07.620 --> 02:13.310 OK, here, all I need is to append a new line character like so, cool! 29 02:13.310 --> 02:15.250 Now it looks better. 30 02:15.320 --> 02:20.860 Also remove the new line from here because it prints an unnecessary new line after the last file name. 31 02:20.920 --> 02:21.500 All right. 32 02:21.980 --> 02:23.400 So far so good. 33 02:23.420 --> 02:25.110 Here is the last step. 34 02:25.130 --> 02:29.350 Now I'm going to write the names slice to a file. As I showed you, 35 02:29.680 --> 02:32.520 I'm going to use the WriteFile function to do that. 36 02:34.770 --> 02:37.120 It expects three parameters. 37 02:37.230 --> 02:41.970 The first one is the name of the file or its path which is a string value. 38 02:42.030 --> 02:44.930 The second one is a byte slice. For this parameter, 39 02:44.940 --> 02:48.660 I'm going to use the names slice that I've been creating so far. 40 02:48.660 --> 02:51.710 And the third one is the permission mode of the file. 41 02:51.720 --> 02:55.030 It determines the file permissions using a numeric value. 42 02:55.950 --> 03:00.270 Let me show you a web page that calculates the file permissions. Here, 43 03:00.450 --> 03:02.850 I'm going to select all the read permissions. 44 03:02.880 --> 03:09.480 It's because I want everyone to read the file that I'm gonna create but I want only my user to have 45 03:09.480 --> 03:11.260 a write permission to the file. 46 03:11.520 --> 03:15.750 So I'm going to select the write permission just for my user like this. 47 03:16.310 --> 03:21.730 This is the octal permission number that I need to use. When a number starts with zero, 48 03:21.740 --> 03:24.020 it means it's an octal number. 49 03:24.020 --> 03:29.790 If you are curious, you can find more information about octal numbers in the resources of this lecture. 50 03:29.810 --> 03:33.230 Okay let me copy it. Here, first, 51 03:33.250 --> 03:39.660 I'm going to write to a file named "out.txt" and I'm going to pass the names slice. 52 03:39.730 --> 03:44.590 And lastly, I'm going to paste the permission number that I've copied. 53 03:44.630 --> 03:49.000 There is no error because Go can work with octal numbers. By the way, 54 03:49.010 --> 03:56.970 the WriteFile function returns an error value. So let's also add error handling like so. Here, 55 03:57.030 --> 04:03.340 if an error occurs it prints the returns the error message and terminates the program. All right. 56 04:03.370 --> 04:10.400 Our program is ready. Let's run it now. As you can see, the program has created the out.txt file. 57 04:10.430 --> 04:12.250 Let's take a look inside. 58 04:12.290 --> 04:13.250 Excellent. 59 04:13.250 --> 04:20.450 It has written the names of the empty files into the out.txt file. So you can use this program 60 04:20.450 --> 04:24.140 to find out empty files in any the rectory within your system. 61 04:24.140 --> 04:24.730 All right. 62 04:24.770 --> 04:25.710 That's all for now. 63 04:25.750 --> 04:27.080 See you in the next lecture, bye!