Welcome! In this lecture,
I'm gonna show you a program which can print unique random numbers.
First I'm going to build it by using arrays.
Then I'm going to refactor it into slices.
Okay let's get started.
Let's seed the rand package first.
I want to create five random numbers. To make it clear,
let's put it into a constant. OK. Let's generate random numbers using a loop. Here,
I'm going to generate a random number like this.
OK let me print it as well..
All right let me run it. As you can see, sometimes the numbers are duplicated, right?
OK.
Now let's search for the unique numbers. I can store the unique numbers in an array like this one. Now,
I'm going to loop over the array like this and here I need to check whether the generated random number
is a duplicate or not. If it's a duplicate,
Then, let's continue to generate more random numbers. By the way,
whenever the program finds a duplicate number, it should generate a new random number instead. To do that,
To do that,
It needs to continue from the parent loop.
So I'm going to label the parent loop like this. Then, I'm going to continue from the parent loop. OK.
If the execution reaches here, it means that the number is unique.
So I'm going to save it into the array, and now I need to move this incdec statement from here to
here. So that it will be incremented only when a unique number is found.
OK.
Now let's print the unique numbers once right after the loop.
Now it finds the unique numbers. Perfect! Let's make the program dynamic by getting this max value from
the command line.
For now I'm going to skip the error handling, but you usually should always handle the errors.
OK.
Here is an error.
Remember I cannot declare the length of an array using a variable.
OK let's change its size manually to 10 for now like this.
OK let's try it ok.
It works but the half of the array is empty.
It's because my program always allocates an array with ten elements even though I only want five unique
random numbers.
Think of what would happen if the array had millions of elements?
This would be a huge waste of space!
Right?
By the way this is not the only problem here.
For example let's pass eleven this time. Now the program crashes because there isn't enough space in
the array. So it is clear that I cannot make this program dynamic using an array.
I need something dynamic something that can change its length dynamically.
You know the answer.
So now let's refactor this program to slices. First, I'm gonna remove the length from here.
So the unique variable is not an array anymore.
It's a slice variable now. Let me run it. Wow! It crashes again.
Let me explain. Here,
this statement tries to assign to a nil slice.
That is why there is an indexing error here, because the slice is nil.
So it doesn't contain any elements.
So instead I'm going to use a built-in function called the append, let me comment out this statement
first.
Now I'm going to use the append function like this.
This function appends the n variable to the unique slice as a new element.
Okay let's try it.
Let's past 10 or let's pass 15 or let's pass 25 or let's pass 50 as you can see it works. The slice dynamically
adjusts its length. It grows value when you append a new element to it. By the way, I'll talk about the append function
in detail afterward, no worries. OK. Now we can refactor this code. Let's start with this found variable.
I don't need it anymore. That's because we can check the length of the uniques slice directly.
This wasn't possible before with an array. It's because, as you know, the length of an array is fixed, however,
a slice's length dynamically changes, so we can use it as in here.
So let's get rid of it from here first.
I'm also going to remove it from this loop as well, here. Instead, I'm going to get the length of the slice
using our old friend the len function like this.
Let's try running it again. Cool! It works! By the way,
almost every function and method in the Go standard library prefer to work with slices instead of arrays.
Let me show you why.
Let's say I want to sort the unique numbers.
There is a function to do that for me in the Go standard library.
It's called sort.Ints. Let's take a look at its documentation. As you can see, it takes an int slice.
So you can pass any int slice to this function no matter what the slice's length is. If this function were
accepting an array instead, it would be harder to use this function. There would be hundreds of duplicate
sort functions just to accept different lengths of arrays like these ones. For example, this function
can on the work with an array that has 50 int elements or this one can only work with an array with a hundred
int elements and so on. So, Instead of this, sort.Ints function expects a slice like this.
This is superior because it can work with a slice of any length.
Anyways let's pass the unique slice to this function.
Let me also print it. Let's try it. As you can see, now the unique numbers are sorted. Cool! By the way, using
this function,
you can also sort an int array such as this one. Let me pass it to the Ints function. Let me also
print the result. As you can see, it doesn't work, but why?
It's because, the nums array's type is [5]int.
But sort.Ints function expects an int slice. It doesn't expect an array.
So what should I do?
Well, I can convert this array to a slice by using a slice expression like this.
Let's run it. Cool! It works! As you can see, an array can also be used like a slice.
In this section, I'll talk about everything you see here in detail by the way.
No worries. This was just another introduction.
I hope you're beginning to understand the power of slices now.
We are still exploring its shores.
Thank you for watching.
See you in the next lecture.