In this review we will learn what slices and maps are less than maps are data structures that hold a
collection of values a slice is not about a collection of elements of the same type.
And they look like this open and close bracket type.
Let's use it open square brackets and the items or elements.
Let's say I have these numbers and 13.
So I need to assign this to some variables.
So let's do number equals to this.
Okay.
Now let's print it to see how it looks like and I have my slice of numbers.
Now as this slice is typed I come up a string here because if I do it the compiler will tell me that
there is an error and the error is I can convert twenty one between quotes which is a string to the
type end.
So remember that when you're doing this all the elements has to be defined in the same type.
Okay.
Now reference elements in disarray.
I will use the square bracket notation I opened square bracket.
I reference the position and I close the square brackets.
Now I'm pointing to the first element.
It's important to know that slices use this index starting from zero to these no one is going to be
in the position zero.
The second number is gonna be in the position one third number is going to be in the position too and
so on.
So if I want to last the last element I'm gonna do numbers sub six.
Now let's say you want to access the position 100 which doesn't exist.
What do you think will happen.
Well I have an error and the error is index which is a hundred.
It's out of range because my slice has just seven element.
So how do I know how many elements does my slice has.
We have a function for that which is called Len in words like this Len of numbers it's gonna be seven
but it's important to in order to access the last element.
I don't use 7 but six because index starting at zero.
So if I want to print the last number of my array of my lines I can do number six.
Are we just so or numbers stop.
Len minus 1 and various not well-defined numbers.
No it's fine.
And I have the last element.
OK.
Now to traverse this slice.
We can use what we've learned in the previous episode which is the for keyword.
So we'll do it by these four range numbers.
Now range number will return to values for each iteration.
The first one is the index and the second one is the value.
So now let's bring the content of those variables in the will be index value will be value and let me
remove this line which is really important right now I made a mistake.
Yeah.
Missing a coin here.
Because two different arguments.
OK so now I have as we saw in the code here possible zero or index zero value one in this one value
one index two values to index three value three and now indexed for value five.
Index 5 value 8 index 6 value 13 the same as we can see here.
Right.
This is pretty useful when you want to use these iterate over this array.
Now let's say you just want the bodies and I don't want the index it's just as easy as use underscore
here to ignore them variable and just print the value right and of course they can do the same.
For index let's say I don't one day in this and I just want the volume so I nor with underscore that
value and I remove it from here.
So I'm just caught up in the index now.
Right now another property for number four slices is that I can add extra values to it.
So let's say I have these numbers and I want to upend the number hundred and of the slice.
I will do it by call a pen.
The first argument is gonna be the line with his numbers and then I get the extra value which is going
to be a hundred.
So now what I'm doing is printing the numbers within append the extra value of one hundred and I have
the numbers which is from 1 to 13 plus an extra hundred.
Now a pen supports a dynamic number of arguments like another 100 hundred Here are nine fifty and so
on.
And if I print these now I'm gonna get the original numbers plus the one that I have on it.
So yeah now let's see what maps are for maps.
It's kind of similar to slices but they are on all that a collection of key value items.
They are also known in other languages as associative arrays House tables or dictionaries have a better
understanding of them.
They looks like a table like Excel table where the first column eats a key and the second one is in
value.
It's just a key value pair it's always a pair pull there's always gonna be a key and there's always
gonna be a value.
So let's say this is my user table I will have three keys and three values.
So how should I define this not by using the map keyword between brackets square brackets I'm going
to define the type of the key using string and then the type of the value let's use a string as well
because here I'm just using strings.
Now I can open and close to curly brackets to define the values.
The key is going to be on the first place let's say name column value.
In this case study and after I need two other trailing columns.
So let's do the same for the other elements.
E-mail is gonna be double our company that come and finally Row developer.
OK.
Now I have this letter saying that you are variable.
So now my user variable points to a map of string keys and string values.
Let's drink this.
See how it looks like.
OK.
No errors.
Can I have my map with three items or elements using string keys as a string of values.
Now let's say I want to access a specific item in this map.
I'll do it by call coal users open square brackets and the key.
So I want to bring the email is using and I have it right now.
What happened.
If I reference a non-existent key so let's say I really want to get the age of these user but it's not
defined right.
I get an empty string so I can either define it like age 30.
Now I should print and write but if I don't have it how can I know that the age is not defined.
So for that we use the decimal with it here but instead of assigning into a single variable we will
use a couple of miles.
So this is very interesting.
If I use a variable a single one I'm just getting the value if it exists.
Otherwise I just get an empty string.
But if I used to write this here I can verify.
OK.
Is true.
So I'll print H age right.
Health or otherwise is OK was false.
I can print age.
Not fun right.
So I reference the age here.
I assigning into the age variable.
But the second one is just gonna be true or false depending if age were defined.
So it says age is not found.
Now let's define age and see what happens.
Great is there.
Now remember the tea cup is case sensitive.
So we if here age is the capital a he's gonna be on phone anyway so I will need to make it the same
in every case.
Okay.
Finally if I want to add some extra data to the user I can do it like this.
And now these were well and the functions that we solve for the license for traversing nice oh also
works.
So if I do for key value call on equal to rage user I will be able to see the value and the keeper on
every iteration of Green Line key key and volume value I have here key role.
Value developer key name volumes tell and so on.
Now it's important to note that every time that I printed this the order of the key are different.
So here I have a name email and roll.
But before I have row name an email.
So these are very important different between maps and slices slices are ordered while maps are not.
So yeah this is the most important thing about maps and slices.
They are pretty much complex that I've just explained to you but those details are not part of this
course.
So if you want to dig more and get more information about them go to the goal of official documentation
and you will see a very complete document that explain how the words and other possibilities that you
have when working with them.