1
00:00:00,880 --> 00:00:05,960
So so far, we've figured out how to create variables, how to create our own types for variables.

2
00:00:06,280 --> 00:00:08,220
We had a brief introduction to pointers.

3
00:00:08,230 --> 00:00:09,520
We know what a struct is.

4
00:00:09,790 --> 00:00:13,570
We have a couple of other data structures, including maps and slices.

5
00:00:14,020 --> 00:00:19,630
Now it's time to look at looping, at going or changing over things.

6
00:00:19,960 --> 00:00:21,710
And again, it's not that difficult.

7
00:00:22,510 --> 00:00:24,150
So let's start with a for loop.

8
00:00:24,160 --> 00:00:28,780
And if you're coming from another programming language, you might be used to multiple kinds of loops.

9
00:00:28,780 --> 00:00:36,340
For example, a for loop a while loop, a do while loop, whatever it may be, go strives for simplicity.

10
00:00:36,550 --> 00:00:42,520
So it has one looping type the for loop because you can do anything you can do in a while, loop with

11
00:00:42,520 --> 00:00:44,340
a for loop and we'll get to that eventually.

12
00:00:44,350 --> 00:00:46,030
But right now let's have a look at a for loop.

13
00:00:47,080 --> 00:00:53,050
So I've cleared out all the code from my mindo go file and all I have left is an empty main main function

14
00:00:53,050 --> 00:00:54,220
and a package declaration.

15
00:00:54,230 --> 00:01:00,670
So let's create a loop and you create a for loop with the word not surprisingly for and you need to

16
00:01:00,670 --> 00:01:01,930
give it a place to start with.

17
00:01:01,930 --> 00:01:05,250
And what we're going to start at is zero.

18
00:01:05,260 --> 00:01:10,090
So we'll create a variable I and we will assign the value of zero to it.

19
00:01:10,300 --> 00:01:12,700
Then a semicolon which you almost never see and go.

20
00:01:12,700 --> 00:01:13,270
But there it is.

21
00:01:13,270 --> 00:01:16,170
We have one, then we have our condition.

22
00:01:16,180 --> 00:01:17,860
When do we want this loop to stop?

23
00:01:17,860 --> 00:01:24,600
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.

24
00:01:25,300 --> 00:01:29,830
So once you get to 11, stop executing and then we have.

25
00:01:29,830 --> 00:01:30,940
What do we do with our counter.

26
00:01:30,940 --> 00:01:38,830
Our counter is EI plus plus which is just a shorthand that says the same thing as I equals I plus one

27
00:01:38,920 --> 00:01:44,620
and then I have curly braces and in between those curly braces is everything I want to do in this for

28
00:01:44,620 --> 00:01:48,340
loop, what I want to do, what we'll do, what we've been doing right along, we'll just print the

29
00:01:48,340 --> 00:01:55,330
terminal log print line and I'll just print the value of it so it automatically imported log for me.

30
00:01:55,870 --> 00:01:57,340
And this is my program.

31
00:01:57,350 --> 00:02:00,310
So what's it going to do while the program starts?

32
00:02:00,310 --> 00:02:02,190
It finds the main package easy.

33
00:02:02,200 --> 00:02:07,690
There's only one package and then it says execute whatever you find in the main function and we're going

34
00:02:07,690 --> 00:02:08,710
to execute a for loop.

35
00:02:08,710 --> 00:02:11,680
And what that for loop does is it initialize is a variable.

36
00:02:11,680 --> 00:02:18,640
I with the value of zero every time it goes for the loop, it adds one to I and it stops when the condition

37
00:02:18,820 --> 00:02:21,370
is less than or equal to ten is no longer true.

38
00:02:21,370 --> 00:02:23,080
In other words, when I equals eleven.

39
00:02:23,260 --> 00:02:25,870
So I should get when I run this go round bingo.

40
00:02:27,580 --> 00:02:34,900
I should get the numbers zero to 10 printed out one per line and I do I get the zero zero right up to

41
00:02:34,900 --> 00:02:35,200
ten.

42
00:02:35,650 --> 00:02:35,950
All right.

43
00:02:35,950 --> 00:02:37,390
So that's what a for loop is.

44
00:02:37,750 --> 00:02:39,220
And it's pretty simple.

45
00:02:39,220 --> 00:02:45,250
I can change this condition if I say if four is less than or equal to one and clear the screen here,

46
00:02:47,020 --> 00:02:51,640
I should only get zero and one and I get zero and one.

47
00:02:51,760 --> 00:02:55,240
So that's all the for loop is simplest thing in the world now.

48
00:02:55,240 --> 00:02:57,280
It's useful to be able to do this.

49
00:02:57,280 --> 00:03:03,010
There are many times when you want to run a particular operation, a certain number of iterations,

50
00:03:03,670 --> 00:03:05,740
but it's also useful to range over data.

51
00:03:06,100 --> 00:03:09,040
So let's create a slice.

52
00:03:09,040 --> 00:03:16,030
So I will say via let's just do the shorthand, I'll save my slice and I'll make a slice of strings

53
00:03:17,170 --> 00:03:20,980
is equal to a slice of string type string.

54
00:03:21,400 --> 00:03:31,070
And in parentheses I'll put dog, cat, horse, fish and banana just to be different, which are probably

55
00:03:31,070 --> 00:03:31,670
going to spell wrong.

56
00:03:31,670 --> 00:03:32,210
No, I got it right.

57
00:03:32,210 --> 00:03:32,620
There we go.

58
00:03:33,220 --> 00:03:36,610
Banana and I want to range over that slice.

59
00:03:36,620 --> 00:03:41,230
In other words, I want to start at the beginning of the slice and I want to print every entry in that

60
00:03:41,230 --> 00:03:42,700
slice to the terminal.

61
00:03:42,910 --> 00:03:50,080
Well, it's not that hard for and I'll put the I here to start with my index and then a value which

62
00:03:50,080 --> 00:03:54,640
I'll just call X is assigned range is a keyword.

63
00:03:55,120 --> 00:04:00,790
We're going to range over this data will range over my slice and then in parentheses we have to do something.

64
00:04:00,790 --> 00:04:05,320
Now, I've created two variables here because the range function actually returns.

65
00:04:05,830 --> 00:04:12,490
Both the current index where you are in the ranging through the data and the X stands for the current

66
00:04:12,490 --> 00:04:14,260
piece of data, what I'm currently looking at.

67
00:04:14,620 --> 00:04:18,810
So I actually don't want the index and I can get rid of that just by using the underscore instead.

68
00:04:18,820 --> 00:04:26,470
So all I have now is X and if I go log print line X there, there's my whole program, pretty straightforward.

69
00:04:26,770 --> 00:04:31,780
And what I'm doing is loading this slice of strings here.

70
00:04:31,780 --> 00:04:36,460
I'm ranging over it and every time I get a value I'm printing that value to the screen.

71
00:04:36,460 --> 00:04:41,080
So let's clear the screen and run this dog.

72
00:04:41,080 --> 00:04:42,860
Cat, horse, fish, banana.

73
00:04:42,940 --> 00:04:43,570
Perfect.

74
00:04:43,750 --> 00:04:50,980
All right, let's change my slice to my map and get rid of this and declare a map.

75
00:04:52,540 --> 00:04:59,590
My map is assigned the value of make a map string.

76
00:05:00,910 --> 00:05:07,840
Which I got a separate string of a map of strings, and I'm using a string to look up my values in the

77
00:05:07,840 --> 00:05:17,560
map and let's put some values in there, my map dog is equal to dog.

78
00:05:19,210 --> 00:05:24,100
Try that again, dog, and I'll duplicate that two more times.

79
00:05:24,940 --> 00:05:27,880
Fish and hat.

80
00:05:32,440 --> 00:05:33,430
I've created a map.

81
00:05:33,730 --> 00:05:36,160
It has three values dog, fish and hat.

82
00:05:36,520 --> 00:05:39,880
And I ranging through that map and I'm putting the information out.

83
00:05:39,880 --> 00:05:46,830
But this time let's put the index in there too, and we'll put I see what we get.

84
00:05:49,670 --> 00:05:50,510
Let me clear the street.

85
00:05:51,890 --> 00:05:58,050
Dog, dog, fish, fish, hot, hot was through the screen and do it again, hot dog, dog fish hates

86
00:05:58,060 --> 00:05:58,750
in a different odor.

87
00:05:58,750 --> 00:06:02,500
Remember I said maps are randomized, maps are randomized.

88
00:06:02,500 --> 00:06:07,480
You cannot depend upon a map being in the same order that the contents of a map will be in the same

89
00:06:07,480 --> 00:06:07,690
order.

90
00:06:07,690 --> 00:06:10,330
You enter the go compiler randomizer.

91
00:06:10,570 --> 00:06:14,020
I don't know why, but they must have some really good reason for doing it.

92
00:06:14,020 --> 00:06:14,650
So they do.

93
00:06:15,130 --> 00:06:18,670
So slices will remain whatever order they are at any given time.

94
00:06:18,670 --> 00:06:21,040
But maps are actually randomized.

95
00:06:21,310 --> 00:06:23,680
But I can range over both types.

96
00:06:23,950 --> 00:06:32,620
And again, if I created up here, if instead of this I had a user struct type user, which is a stretch

97
00:06:32,620 --> 00:06:42,580
to create my own data type and I put in first name string, last name string, and then I create instead

98
00:06:42,580 --> 00:06:53,710
of a map here of I mean this is slice my slice and create my slice as my slice of VA.

99
00:06:54,340 --> 00:06:58,450
My slice is a type user.

100
00:07:05,370 --> 00:07:07,350
I make a slice of users.

101
00:07:09,180 --> 00:07:11,170
OK, there we are.

102
00:07:11,220 --> 00:07:16,830
When I have a typo up here, what's my typo, first name, unresolved type, first name is of type string.

103
00:07:16,830 --> 00:07:17,640
I don't need Colon's.

104
00:07:17,640 --> 00:07:19,860
That's why I do that all the time.

105
00:07:20,280 --> 00:07:20,760
All right.

106
00:07:21,180 --> 00:07:25,880
So now I actually have a slice and it says here it's Skip because my slice is always nil.

107
00:07:25,890 --> 00:07:26,330
I know.

108
00:07:26,340 --> 00:07:27,420
Let's put some value in there.

109
00:07:27,810 --> 00:07:35,670
We'll put you one is of type user analysts put the values in there.

110
00:07:39,120 --> 00:07:45,300
First name Trevor, and I'll leave the second name empty for now, I just want to show you can range

111
00:07:45,300 --> 00:07:48,330
over this since the string, I got to put quotation marks around it.

112
00:07:48,360 --> 00:07:51,990
I think it's time for a coffee and then I'll create a user to.

113
00:07:56,270 --> 00:08:06,140
And we'll call this person Sam, and now open those to my slice, my slice equals prepend my slice,

114
00:08:06,170 --> 00:08:10,280
you one, and then I'll append you two to it as well.

115
00:08:11,330 --> 00:08:18,470
So now I have a slice of strings, a slice of users, which has a couple of values put in there.

116
00:08:18,470 --> 00:08:19,900
And now I should be able to range over this.

117
00:08:19,910 --> 00:08:25,010
So I've created a slice, my slice, which is a slice of type user users to find up here.

118
00:08:25,490 --> 00:08:29,350
I've created two users, only bothered populating the first field, but that's OK.

119
00:08:29,360 --> 00:08:31,030
You can put the second field in if you wish to.

120
00:08:31,490 --> 00:08:33,320
And now I can iterate through this.

121
00:08:33,470 --> 00:08:36,920
Now, if I leave it like this and run it, here's what I get.

122
00:08:38,600 --> 00:08:40,670
And it shows me what's in each of those.

123
00:08:40,670 --> 00:08:46,820
But if I actually refer to the fields that are in that struct first name and run it again, now, it

124
00:08:46,820 --> 00:08:50,900
will just print out the first name of each of those person for each of those people, plus the index,

125
00:08:50,930 --> 00:08:52,010
because I left that turned on.

126
00:08:53,020 --> 00:08:53,350
All right.

127
00:08:53,360 --> 00:08:54,770
That's how you range through data.

128
00:08:54,770 --> 00:09:01,970
You can use a for loop or you can use this this syntax here for index value is assigned range through

129
00:09:02,300 --> 00:09:03,920
whatever your structure happens to be.

130
00:09:04,640 --> 00:09:06,260
As long as your structure is changeable.

131
00:09:06,260 --> 00:09:09,080
Of course, you can't range through one variable of one type.

132
00:09:09,080 --> 00:09:13,490
So I can't range through a string because that's not a Wrangel data structure.

133
00:09:13,580 --> 00:09:18,060
But maps are, slices are and others are, as we'll see as time goes on.

134
00:09:18,440 --> 00:09:18,800
All right.

135
00:09:18,920 --> 00:09:21,920
That's it for ranging through data and looping through data.
