Welcome back!
In this lecture, I'm going to write the name of the empty files to another file.
All right! Let's get started.
To save the names of the empty files to a file, I need another function from the Go standard
library.
It's in the ioutil package.
Let me show you. As I said in the previous lecture, ioutil package has a lot of practical functions
for working with files.
For now, I'm only interested in the WriteFile function. WriteFile function
basically writes what's inside the given byte slice to a file.
It's that simple. So let's create a slice to store all the file names.
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
slice, like this one.
Now I'm going to append the file name to the names slice like so. However, there is an error.
It says that the name is a string but the names slice's element type is a byte.
It is because the names slice is a byte slice.
Remember you can only append a value to a slice with an identical type of that slice's element type.
OK?
So how can I convert the name string to byte values.
Well, it's like this, I'll talk about this later in detail but for now let tell you that a string
value is actually a bytes slice behind the scenes. Here,
the name string value gets converted to a byte slice on the fly.
Then I'm using the ellipses operator to append the elements in that bytes slice to the names slice.
It's like appending the individual byte values manually.
All right, let me print it
using the s verb. This s verb converts the given bytes slice to a string value automatically. OK, let's run
it. Oops! It doesn't print new lines after the file names. But, why?! Please pause the video and try to solve
this problem.
OK, here, all I need is to append a new line character like so, cool!
Now it looks better.
Also remove the new line from here because it prints an unnecessary new line after the last file name.
All right.
So far so good.
Here is the last step.
Now I'm going to write the names slice to a file. As I showed you,
I'm going to use the WriteFile function to do that.
It expects three parameters.
The first one is the name of the file or its path which is a string value.
The second one is a byte slice. For this parameter,
I'm going to use the names slice that I've been creating so far.
And the third one is the permission mode of the file.
It determines the file permissions using a numeric value.
Let me show you a web page that calculates the file permissions. Here,
I'm going to select all the read permissions.
It's because I want everyone to read the file that I'm gonna create but I want only my user to have
a write permission to the file.
So I'm going to select the write permission just for my user like this.
This is the octal permission number that I need to use. When a number starts with zero,
it means it's an octal number.
If you are curious, you can find more information about octal numbers in the resources of this lecture.
Okay let me copy it. Here, first,
I'm going to write to a file named "out.txt" and I'm going to pass the names slice.
And lastly, I'm going to paste the permission number that I've copied.
There is no error because Go can work with octal numbers. By the way,
the WriteFile function returns an error value. So let's also add error handling like so. Here,
if an error occurs it prints the returns the error message and terminates the program. All right.
Our program is ready. Let's run it now. As you can see, the program has created the out.txt file.
Let's take a look inside.
Excellent.
It has written the names of the empty files into the out.txt file. So you can use this program
to find out empty files in any the rectory within your system.
All right.
That's all for now.
See you in the next lecture, bye!