So this time around, I want to look at a couple of different data structures, half of what you'll
be doing when you're writing programs is figuring out what kind of structure you want to store your
data.
And so far, we've seen some simple variables.
And what I have here is just I've I've just taken everything out of the main function, deleted all
the all of the other functions.
And I just have one go file mango in package main with an empty main function.
So what we've seen so far is just how to create variables so we can create a string variable like this
via my string type string or we can create another variable var my input type it and we can store things
in those by going my string is equal, too high and my input is equal to 11 and we have float's and
other types as well.
We also learned a shorthand for declaring a variable like my second string is equal to
another string.
And of course, the assignment variable has to have a colon before the equals sign, and then we can
print those things out.
Log, print line and just put all the variables in there, my string, my second string and my aunt.
And that program will run just fine.
So if I run that go, run, maned go, it'll just print those variables out as it does.
So that's one way of storing data in simple variables.
But sometimes you don't want to do that.
Sometimes you want to have different data structures and we're going to look at to this time around.
So let me delete all of this and show you how to create a map.
First of all, what is a map?
Well, let's make one.
We'll call it my map.
And I'll do the shorthand is equal to make a map, string, string.
So I've created a map, but what is it?
Well, the syntax for creating it is like this.
You can if you want to go over my other map, map, string, string.
But what you've done there is you've created a map that you can't actually do anything with.
You don't create maps that way.
There may be a certain really rarified set of circumstances where you'll find you find that an appropriate
action.
But I have yet to encounter it.
So this is not how you declare a map.
I'm going to get rid of that because that's not how you do it.
You create a map this way using the shorthand syntax as the most common way of doing it.
You use the make keyword and it's a built in function.
You can read about it here just by rolling your mouse over it and reading that if you wish.
But you actually create a map using this kind of of of syntax.
So the first thing what you see in the square brackets here is actually the index that you use to look
up the map.
And what's following it is the value that is stored at that index.
So I can do this now, my map dog and then my dog's name is equal to Sampson and then I can print that
log dot print line, my map dog, which I better use the right case because in this case center and
if I run this program, which I'll do right now, go run main go, it'll print out Sampson.
What it's what I'm telling log print line to do is go to the map my map, find the key in that map called
dog and print the value of it.
I could even add something else to this.
My map other dog is assigned the value of Cassi and if I duplicate this line and look up that key other
dog, then it will print out first of all, Sampson, then Casy go run.
Bingo, Sampson, Cassi.
I can store as many things as I want in this map and I'm always going to store it using that key.
So now what happens if I overwrite one of the keys?
My map dog is equal to phyto.
Well, what do you think's going to happen exactly?
What's going to happen is exactly what you think is going to happen.
It's going to replace the value of the key dog.
It used to be Sampson.
It'll put Fido there instead.
So when I run this program now, go, run main go, it'll print Fido and Kassi.
So that's what a basic string map is.
But you can have other types of maps, so let's delete everything here.
And instead of saying my map is a type of map, string, string, I can make it, my map is a type of
map, string it and then I can put a couple of values in my map.
First is going to equal to one and my map second is going to equal to two.
And now if I put my log print line in here, logged print line my map first and then I print right after
that, my map.
Second, with a capital S. It'll print out one and then two, let's run it and make sure it does, I'll
clear the screen.
Go Roumain, go one, two.
Exactly what you want.
Now a map can store anything.
So if, for example, I get rid of this and get rid of this and define my own type so I'll make a type
type user is a struct.
And inside of that, I have first name, which is a string and last name, which is a string, and then
down here I made it make this a type of map, string, yuzu.
I can do that.
I create my own data type and I can store that in a string.
So if I create a user, so I'll say me is assigned assigned type of user.
And in that I put first name Trevor.
Last name Sawalha.
And then put a comma at the end, so it passes and then I say my map to me is equal to me and then log
that print line.
I can look up that value by saying my map.
And the key, it is me dot first name.
And if I run this go run mango, it should protect out Trevor, and it does so you can store whatever
you want in a map, even types that you create yourself and maps are extremely useful because they're
very, very fast.
And one of the other nice things about maps is they're immutable.
So in a lot of cases, when you're passing information around in a program, for example, you might
create a structure or a variable or whatever it is.
So let's just create a var my new car is of type.
Oh, let's call it float 32 and my new Neuvirth
is equal to eleven point one.
No problem.
I might want to pass that value, but I might want to pass a pointer to that value because I want to
make sure when I pass that variable around to another function or another package that I'm actually
passing a pointer to it.
So when it's changed in the other package or changed in another function, I know it's going to affect
the one that I created here.
But you don't have to do that with maps because maps stay the same no matter where they go.
You never have to bother pointing a passing a pointer to a map.
You can just pass the map itself and that map will remain constant no matter where in the program it
is accessed.
That's extremely useful.
Another point to know about maps is maps are programmatically built into the system, not sorted.
So if you put things into a map in a certain order and then you try to access them later on, you cannot
assume that they are going to be in the order.
You originally added that you always must look up by key.
That's a key thing to bear in mind.
It used to be in the early days ago that people found a way that the hey, if I put things in a map,
I can always get them out in the same order I put them in.
And then the creators of Go said, no, we don't want you to do that.
And in fact, we're going to sort them or we're going to randomize them on purpose.
So you can't depend upon a map always being in the same order.
So that's one thing to bear in mind about maps.
OK, so maps are pretty straightforward and you always make it using this syntax make map.
What do you want to look it up by?
I usually use a string in most cases because it's easy to look up things by strings, but you can put
an integer in there if you want to or whatever you want.
Anything you can use as unique key for that map and then you can store whatever data type you want in
there.
If you don't know what data type you you're going to be storing in there, you can in fact, and this
is not recommended, but you can put in type interface followed by an opening and closing curly brackets,
and that will actually store anything you want.
The problem is you have to cast it from what it is in the map back into what it needs to be in order
to be useful for you.
So this is really kind of a kludge, and I wouldn't do that.
I would always use something else.
So in here I had a string.
I can put it back to a string and now or how to use or I guess and now everything works.
All right.
So that's maps.
Let's get rid of this and let's talk about slices.
Another thing you're going to run into quite regularly now from other programming languages.
You're probably if you've worked in another programming language, you're probably familiar with the
concept of an array and an array is is a data structure that can hold many elements just like a map,
except that an array can be sorted but can go.
We almost never use arrays.
Instead we use slices.
And here's how it works.
So remember, we can we can create a variable that's a string by saying via my string of type string.
And in that I can I can store one value so I can put my string is equal to fish and that's fine.
No semicolon and then I can print that out log, print line my string so we know what that's going to
do.
We've done that many times already.
But what happens if you want to store more than one string in a variable so you might have a selection
of users, for example.
So they might be just first names.
So I want to have a variable that will store more than one string, which happens to be a first name
in the same place.
Well, I can do that simply by putting these characters in front of the variable name.
This is now a slice of strings, which means I can put more than one thing into it.
Well, how do you add something to a slice that's not actually not that hard?
I can simply say my string, which I'm going to rename to my slice, we'll call it my slice, rename
it here equals append to put on the end of the slice.
The slice I want to append to which is my slice.
It's the only thing I have here and some value so.
My name there and now what happens if I try to print that slice out?
Let's run this program and find out.
Go run Mango and it prints out.
See how it put the braces, the square brackets around it, Trevor.
Well, let's put another value in the string, John, and now run it again and I'll clear the screen
so we can see it more easily.
Now, it has two values, Trevor and John.
I can put a third value in there, Mary and run it again.
Go Roumain.
Go Trevor.
John and Mary.
So now I have a data type called a slice, and this happens to be a slice of strings where I can put
multiple values.
Well, what if I change this from a slice of strings to a slice of it?
Well, first of all, I only get three errors, but I can put two.
One.
And three and now if I run this, I should see two, one, three in exactly the order that I added them
to one three.
That was great.
Can I do anything to a slice while we can find out really easily type of my slice followed by Duck and
then we have append a time to sign cap copy for four.
We have all kinds of things we can do to it.
Pretty line.
What's this one sought.
Let's try sorting it.
Sort in my slice.
Well, let's store that somewhere.
Sorted is assigned sort since my slice.
No, that's not what it does.
Let's try it again.
Let's just run it like this is it looks like it's actually going to sort the.
It's in my slice.
Let's run the program now.
Last time we got two, one, three, the order we put them in.
This time we get one, two, three.
Look at that.
I can sort a slice that works really well.
So slices are extremely useful.
And the great thing about them is you can declare it and then put as many values as you want in there.
So that's an extremely helpful feature.
Now, there are other ways of actually declaring slices, for example, I can use that shorthand, I
can say, for example, numbers he's assigned and this time I'm going to go slice of it and then I will
put some numbers in there.
So one, two, three, four, five, six, seven, eight, nine, eight, eight, nine and 10.
So now I have a slice.
I've used the shorthand to actually declare a slice of INTs and then I pre populated it with ten values.
So if I put that out, I can go log the print line numbers and reformat to make sure that my imports
are right.
They are.
And that should print out the numbers from one to 10 with square brackets around it.
And it does.
What if I only want to say look at a range of that?
Well, I can do that.
Let's say I want to print the first three.
I can say log print line and this time I want to go numbers, but only the first three and slices like
arrays start counting at zero.
So I can say from zero to two.
And when I run the program this time I get just the first two numbers.
I can go somewhere in the middle, I can go from six to eight or nine.
I guess I typed nine.
So we're going to go with nine, which should give me the last few bits.
And it does seven, eight, nine.
All right, so that works reasonably well and slices will be extremely helpful for you.
And just as you can have a map of different types, we can have a slice of different types.
So if I delete everything and go, my child, do use the shorthand again, names is equal to a slice
of string.
And then in curly brackets, I put in some strings, one seven fish cap.
Now I've created a slice of strings and I can actually print that out, a catalog print line and I'll
print out names.
And if I run this, I'll clear the screen.
Run the program.
Away we go.
One seven fish cat so slices you'll be using them all the time will be using them regularly as we go
through this course and we start, for example, pulling information out of the database.
Sometimes when you access one thing from a database, you want one thing from a database, but very
often you want to say go to the user's table and get me all of the users that have the status of inactive.
And what we're going to give back is a slice of users in that case.
But we got a little bit to cover before we can get that far along.
But for right now, we have two new data types at our disposal, maps and slices.