So so far, we've figured out how to create variables, how to create our own types for variables.
We had a brief introduction to pointers.
We know what a struct is.
We have a couple of other data structures, including maps and slices.
Now it's time to look at looping, at going or changing over things.
And again, it's not that difficult.
So let's start with a for loop.
And if you're coming from another programming language, you might be used to multiple kinds of loops.
For example, a for loop a while loop, a do while loop, whatever it may be, go strives for simplicity.
So it has one looping type the for loop because you can do anything you can do in a while, loop with
a for loop and we'll get to that eventually.
But right now let's have a look at a for loop.
So I've cleared out all the code from my mindo go file and all I have left is an empty main main function
and a package declaration.
So let's create a loop and you create a for loop with the word not surprisingly for and you need to
give it a place to start with.
And what we're going to start at is zero.
So we'll create a variable I and we will assign the value of zero to it.
Then a semicolon which you almost never see and go.
But there it is.
We have one, then we have our condition.
When do we want this loop to stop?
And I will say stop this loop when I is less than or keep it going when I is less than or equal to ten.
So once you get to 11, stop executing and then we have.
What do we do with our counter.
Our counter is EI plus plus which is just a shorthand that says the same thing as I equals I plus one
and then I have curly braces and in between those curly braces is everything I want to do in this for
loop, what I want to do, what we'll do, what we've been doing right along, we'll just print the
terminal log print line and I'll just print the value of it so it automatically imported log for me.
And this is my program.
So what's it going to do while the program starts?
It finds the main package easy.
There's only one package and then it says execute whatever you find in the main function and we're going
to execute a for loop.
And what that for loop does is it initialize is a variable.
I with the value of zero every time it goes for the loop, it adds one to I and it stops when the condition
is less than or equal to ten is no longer true.
In other words, when I equals eleven.
So I should get when I run this go round bingo.
I should get the numbers zero to 10 printed out one per line and I do I get the zero zero right up to
ten.
All right.
So that's what a for loop is.
And it's pretty simple.
I can change this condition if I say if four is less than or equal to one and clear the screen here,
I should only get zero and one and I get zero and one.
So that's all the for loop is simplest thing in the world now.
It's useful to be able to do this.
There are many times when you want to run a particular operation, a certain number of iterations,
but it's also useful to range over data.
So let's create a slice.
So I will say via let's just do the shorthand, I'll save my slice and I'll make a slice of strings
is equal to a slice of string type string.
And in parentheses I'll put dog, cat, horse, fish and banana just to be different, which are probably
going to spell wrong.
No, I got it right.
There we go.
Banana and I want to range over that slice.
In other words, I want to start at the beginning of the slice and I want to print every entry in that
slice to the terminal.
Well, it's not that hard for and I'll put the I here to start with my index and then a value which
I'll just call X is assigned range is a keyword.
We're going to range over this data will range over my slice and then in parentheses we have to do something.
Now, I've created two variables here because the range function actually returns.
Both the current index where you are in the ranging through the data and the X stands for the current
piece of data, what I'm currently looking at.
So I actually don't want the index and I can get rid of that just by using the underscore instead.
So all I have now is X and if I go log print line X there, there's my whole program, pretty straightforward.
And what I'm doing is loading this slice of strings here.
I'm ranging over it and every time I get a value I'm printing that value to the screen.
So let's clear the screen and run this dog.
Cat, horse, fish, banana.
Perfect.
All right, let's change my slice to my map and get rid of this and declare a map.
My map is assigned the value of make a map string.
Which I got a separate string of a map of strings, and I'm using a string to look up my values in the
map and let's put some values in there, my map dog is equal to dog.
Try that again, dog, and I'll duplicate that two more times.
Fish and hat.
I've created a map.
It has three values dog, fish and hat.
And I ranging through that map and I'm putting the information out.
But this time let's put the index in there too, and we'll put I see what we get.
Let me clear the street.
Dog, dog, fish, fish, hot, hot was through the screen and do it again, hot dog, dog fish hates
in a different odor.
Remember I said maps are randomized, maps are randomized.
You cannot depend upon a map being in the same order that the contents of a map will be in the same
order.
You enter the go compiler randomizer.
I don't know why, but they must have some really good reason for doing it.
So they do.
So slices will remain whatever order they are at any given time.
But maps are actually randomized.
But I can range over both types.
And again, if I created up here, if instead of this I had a user struct type user, which is a stretch
to create my own data type and I put in first name string, last name string, and then I create instead
of a map here of I mean this is slice my slice and create my slice as my slice of VA.
My slice is a type user.
I make a slice of users.
OK, there we are.
When I have a typo up here, what's my typo, first name, unresolved type, first name is of type string.
I don't need Colon's.
That's why I do that all the time.
All right.
So now I actually have a slice and it says here it's Skip because my slice is always nil.
I know.
Let's put some value in there.
We'll put you one is of type user analysts put the values in there.
First name Trevor, and I'll leave the second name empty for now, I just want to show you can range
over this since the string, I got to put quotation marks around it.
I think it's time for a coffee and then I'll create a user to.
And we'll call this person Sam, and now open those to my slice, my slice equals prepend my slice,
you one, and then I'll append you two to it as well.
So now I have a slice of strings, a slice of users, which has a couple of values put in there.
And now I should be able to range over this.
So I've created a slice, my slice, which is a slice of type user users to find up here.
I've created two users, only bothered populating the first field, but that's OK.
You can put the second field in if you wish to.
And now I can iterate through this.
Now, if I leave it like this and run it, here's what I get.
And it shows me what's in each of those.
But if I actually refer to the fields that are in that struct first name and run it again, now, it
will just print out the first name of each of those person for each of those people, plus the index,
because I left that turned on.
All right.
That's how you range through data.
You can use a for loop or you can use this this syntax here for index value is assigned range through
whatever your structure happens to be.
As long as your structure is changeable.
Of course, you can't range through one variable of one type.
So I can't range through a string because that's not a Wrangel data structure.
But maps are, slices are and others are, as we'll see as time goes on.
All right.
That's it for ranging through data and looping through data.