1
00:00:00,990 --> 00:00:05,130
So this time around, I want to look at a couple of different data structures, half of what you'll

2
00:00:05,130 --> 00:00:09,000
be doing when you're writing programs is figuring out what kind of structure you want to store your

3
00:00:09,000 --> 00:00:09,300
data.

4
00:00:09,310 --> 00:00:12,310
And so far, we've seen some simple variables.

5
00:00:12,330 --> 00:00:16,800
And what I have here is just I've I've just taken everything out of the main function, deleted all

6
00:00:16,800 --> 00:00:18,030
the all of the other functions.

7
00:00:18,030 --> 00:00:23,520
And I just have one go file mango in package main with an empty main function.

8
00:00:23,550 --> 00:00:28,800
So what we've seen so far is just how to create variables so we can create a string variable like this

9
00:00:28,800 --> 00:00:38,910
via my string type string or we can create another variable var my input type it and we can store things

10
00:00:38,910 --> 00:00:48,360
in those by going my string is equal, too high and my input is equal to 11 and we have float's and

11
00:00:48,360 --> 00:00:49,170
other types as well.

12
00:00:49,200 --> 00:00:57,990
We also learned a shorthand for declaring a variable like my second string is equal to

13
00:01:01,260 --> 00:01:02,640
another string.

14
00:01:06,030 --> 00:01:10,620
And of course, the assignment variable has to have a colon before the equals sign, and then we can

15
00:01:10,620 --> 00:01:11,620
print those things out.

16
00:01:12,000 --> 00:01:22,560
Log, print line and just put all the variables in there, my string, my second string and my aunt.

17
00:01:23,790 --> 00:01:25,700
And that program will run just fine.

18
00:01:25,710 --> 00:01:30,990
So if I run that go, run, maned go, it'll just print those variables out as it does.

19
00:01:32,220 --> 00:01:36,450
So that's one way of storing data in simple variables.

20
00:01:36,840 --> 00:01:39,540
But sometimes you don't want to do that.

21
00:01:39,540 --> 00:01:43,300
Sometimes you want to have different data structures and we're going to look at to this time around.

22
00:01:43,320 --> 00:01:48,110
So let me delete all of this and show you how to create a map.

23
00:01:48,120 --> 00:01:49,620
First of all, what is a map?

24
00:01:49,650 --> 00:01:50,430
Well, let's make one.

25
00:01:51,180 --> 00:01:53,010
We'll call it my map.

26
00:01:58,240 --> 00:02:06,450
And I'll do the shorthand is equal to make a map, string, string.

27
00:02:07,270 --> 00:02:09,790
So I've created a map, but what is it?

28
00:02:09,820 --> 00:02:12,580
Well, the syntax for creating it is like this.

29
00:02:12,700 --> 00:02:22,630
You can if you want to go over my other map, map, string, string.

30
00:02:23,290 --> 00:02:26,710
But what you've done there is you've created a map that you can't actually do anything with.

31
00:02:26,740 --> 00:02:28,840
You don't create maps that way.

32
00:02:29,440 --> 00:02:35,230
There may be a certain really rarified set of circumstances where you'll find you find that an appropriate

33
00:02:35,230 --> 00:02:35,640
action.

34
00:02:36,040 --> 00:02:37,590
But I have yet to encounter it.

35
00:02:37,960 --> 00:02:39,700
So this is not how you declare a map.

36
00:02:39,730 --> 00:02:41,770
I'm going to get rid of that because that's not how you do it.

37
00:02:42,760 --> 00:02:46,690
You create a map this way using the shorthand syntax as the most common way of doing it.

38
00:02:46,900 --> 00:02:50,310
You use the make keyword and it's a built in function.

39
00:02:50,320 --> 00:02:53,860
You can read about it here just by rolling your mouse over it and reading that if you wish.

40
00:02:54,340 --> 00:02:59,370
But you actually create a map using this kind of of of syntax.

41
00:02:59,890 --> 00:03:05,470
So the first thing what you see in the square brackets here is actually the index that you use to look

42
00:03:05,470 --> 00:03:06,160
up the map.

43
00:03:06,820 --> 00:03:11,410
And what's following it is the value that is stored at that index.

44
00:03:11,710 --> 00:03:22,570
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

45
00:03:22,720 --> 00:03:31,720
log dot print line, my map dog, which I better use the right case because in this case center and

46
00:03:31,720 --> 00:03:38,890
if I run this program, which I'll do right now, go run main go, it'll print out Sampson.

47
00:03:39,220 --> 00:03:46,720
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

48
00:03:46,720 --> 00:03:48,760
dog and print the value of it.

49
00:03:49,990 --> 00:03:51,610
I could even add something else to this.

50
00:03:52,840 --> 00:04:05,230
My map other dog is assigned the value of Cassi and if I duplicate this line and look up that key other

51
00:04:05,350 --> 00:04:11,140
dog, then it will print out first of all, Sampson, then Casy go run.

52
00:04:11,140 --> 00:04:13,090
Bingo, Sampson, Cassi.

53
00:04:13,480 --> 00:04:20,210
I can store as many things as I want in this map and I'm always going to store it using that key.

54
00:04:21,190 --> 00:04:24,880
So now what happens if I overwrite one of the keys?

55
00:04:25,150 --> 00:04:29,910
My map dog is equal to phyto.

56
00:04:30,430 --> 00:04:32,560
Well, what do you think's going to happen exactly?

57
00:04:32,710 --> 00:04:35,380
What's going to happen is exactly what you think is going to happen.

58
00:04:35,470 --> 00:04:38,770
It's going to replace the value of the key dog.

59
00:04:39,460 --> 00:04:40,720
It used to be Sampson.

60
00:04:40,720 --> 00:04:42,150
It'll put Fido there instead.

61
00:04:42,160 --> 00:04:47,770
So when I run this program now, go, run main go, it'll print Fido and Kassi.

62
00:04:48,580 --> 00:04:51,070
So that's what a basic string map is.

63
00:04:51,070 --> 00:04:54,400
But you can have other types of maps, so let's delete everything here.

64
00:04:54,550 --> 00:04:59,890
And instead of saying my map is a type of map, string, string, I can make it, my map is a type of

65
00:04:59,890 --> 00:05:05,770
map, string it and then I can put a couple of values in my map.

66
00:05:07,270 --> 00:05:18,070
First is going to equal to one and my map second is going to equal to two.

67
00:05:18,880 --> 00:05:29,530
And now if I put my log print line in here, logged print line my map first and then I print right after

68
00:05:29,530 --> 00:05:30,880
that, my map.

69
00:05:32,850 --> 00:05:39,300
Second, with a capital S. It'll print out one and then two, let's run it and make sure it does, I'll

70
00:05:39,320 --> 00:05:40,130
clear the screen.

71
00:05:40,130 --> 00:05:42,620
Go Roumain, go one, two.

72
00:05:42,620 --> 00:05:43,580
Exactly what you want.

73
00:05:43,910 --> 00:05:46,130
Now a map can store anything.

74
00:05:46,640 --> 00:05:56,810
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

75
00:05:59,360 --> 00:06:02,180
type user is a struct.

76
00:06:04,250 --> 00:06:12,920
And inside of that, I have first name, which is a string and last name, which is a string, and then

77
00:06:12,920 --> 00:06:18,530
down here I made it make this a type of map, string, yuzu.

78
00:06:19,040 --> 00:06:19,700
I can do that.

79
00:06:20,060 --> 00:06:23,020
I create my own data type and I can store that in a string.

80
00:06:23,030 --> 00:06:28,990
So if I create a user, so I'll say me is assigned assigned type of user.

81
00:06:30,080 --> 00:06:34,550
And in that I put first name Trevor.

82
00:06:36,830 --> 00:06:39,590
Last name Sawalha.

83
00:06:42,010 --> 00:06:53,290
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

84
00:06:53,290 --> 00:06:54,320
that print line.

85
00:06:54,370 --> 00:06:57,820
I can look up that value by saying my map.

86
00:06:59,320 --> 00:07:04,810
And the key, it is me dot first name.

87
00:07:07,460 --> 00:07:14,120
And if I run this go run mango, it should protect out Trevor, and it does so you can store whatever

88
00:07:14,120 --> 00:07:20,330
you want in a map, even types that you create yourself and maps are extremely useful because they're

89
00:07:20,330 --> 00:07:21,470
very, very fast.

90
00:07:22,040 --> 00:07:25,040
And one of the other nice things about maps is they're immutable.

91
00:07:25,520 --> 00:07:32,330
So in a lot of cases, when you're passing information around in a program, for example, you might

92
00:07:32,330 --> 00:07:35,280
create a structure or a variable or whatever it is.

93
00:07:35,300 --> 00:07:41,450
So let's just create a var my new car is of type.

94
00:07:41,960 --> 00:07:46,760
Oh, let's call it float 32 and my new Neuvirth

95
00:07:49,580 --> 00:07:52,970
is equal to eleven point one.

96
00:07:53,210 --> 00:07:53,900
No problem.

97
00:07:54,170 --> 00:07:58,910
I might want to pass that value, but I might want to pass a pointer to that value because I want to

98
00:07:58,910 --> 00:08:04,220
make sure when I pass that variable around to another function or another package that I'm actually

99
00:08:04,220 --> 00:08:05,590
passing a pointer to it.

100
00:08:05,600 --> 00:08:11,300
So when it's changed in the other package or changed in another function, I know it's going to affect

101
00:08:11,300 --> 00:08:12,680
the one that I created here.

102
00:08:13,130 --> 00:08:18,290
But you don't have to do that with maps because maps stay the same no matter where they go.

103
00:08:18,290 --> 00:08:21,670
You never have to bother pointing a passing a pointer to a map.

104
00:08:21,690 --> 00:08:27,500
You can just pass the map itself and that map will remain constant no matter where in the program it

105
00:08:27,500 --> 00:08:28,190
is accessed.

106
00:08:28,370 --> 00:08:29,630
That's extremely useful.

107
00:08:30,620 --> 00:08:38,350
Another point to know about maps is maps are programmatically built into the system, not sorted.

108
00:08:38,690 --> 00:08:44,750
So if you put things into a map in a certain order and then you try to access them later on, you cannot

109
00:08:44,750 --> 00:08:47,720
assume that they are going to be in the order.

110
00:08:47,720 --> 00:08:50,980
You originally added that you always must look up by key.

111
00:08:51,200 --> 00:08:52,940
That's a key thing to bear in mind.

112
00:08:53,960 --> 00:08:58,580
It used to be in the early days ago that people found a way that the hey, if I put things in a map,

113
00:08:58,580 --> 00:09:01,190
I can always get them out in the same order I put them in.

114
00:09:01,460 --> 00:09:04,970
And then the creators of Go said, no, we don't want you to do that.

115
00:09:04,970 --> 00:09:09,650
And in fact, we're going to sort them or we're going to randomize them on purpose.

116
00:09:09,800 --> 00:09:13,520
So you can't depend upon a map always being in the same order.

117
00:09:13,700 --> 00:09:16,220
So that's one thing to bear in mind about maps.

118
00:09:16,550 --> 00:09:21,920
OK, so maps are pretty straightforward and you always make it using this syntax make map.

119
00:09:22,190 --> 00:09:23,660
What do you want to look it up by?

120
00:09:23,900 --> 00:09:28,310
I usually use a string in most cases because it's easy to look up things by strings, but you can put

121
00:09:28,310 --> 00:09:30,320
an integer in there if you want to or whatever you want.

122
00:09:30,320 --> 00:09:37,730
Anything you can use as unique key for that map and then you can store whatever data type you want in

123
00:09:37,730 --> 00:09:37,940
there.

124
00:09:38,180 --> 00:09:42,890
If you don't know what data type you you're going to be storing in there, you can in fact, and this

125
00:09:42,890 --> 00:09:48,770
is not recommended, but you can put in type interface followed by an opening and closing curly brackets,

126
00:09:48,770 --> 00:09:51,410
and that will actually store anything you want.

127
00:09:52,130 --> 00:09:58,640
The problem is you have to cast it from what it is in the map back into what it needs to be in order

128
00:09:58,640 --> 00:10:00,270
to be useful for you.

129
00:10:00,290 --> 00:10:03,620
So this is really kind of a kludge, and I wouldn't do that.

130
00:10:03,620 --> 00:10:05,270
I would always use something else.

131
00:10:05,280 --> 00:10:07,190
So in here I had a string.

132
00:10:07,190 --> 00:10:12,920
I can put it back to a string and now or how to use or I guess and now everything works.

133
00:10:13,160 --> 00:10:13,540
All right.

134
00:10:14,300 --> 00:10:15,220
So that's maps.

135
00:10:15,230 --> 00:10:17,960
Let's get rid of this and let's talk about slices.

136
00:10:17,960 --> 00:10:22,760
Another thing you're going to run into quite regularly now from other programming languages.

137
00:10:22,760 --> 00:10:26,660
You're probably if you've worked in another programming language, you're probably familiar with the

138
00:10:26,660 --> 00:10:34,820
concept of an array and an array is is a data structure that can hold many elements just like a map,

139
00:10:34,820 --> 00:10:38,600
except that an array can be sorted but can go.

140
00:10:38,600 --> 00:10:41,060
We almost never use arrays.

141
00:10:41,420 --> 00:10:43,220
Instead we use slices.

142
00:10:43,430 --> 00:10:44,510
And here's how it works.

143
00:10:44,540 --> 00:10:50,780
So remember, we can we can create a variable that's a string by saying via my string of type string.

144
00:10:51,200 --> 00:10:59,570
And in that I can I can store one value so I can put my string is equal to fish and that's fine.

145
00:10:59,750 --> 00:11:07,430
No semicolon and then I can print that out log, print line my string so we know what that's going to

146
00:11:07,430 --> 00:11:07,600
do.

147
00:11:07,610 --> 00:11:09,200
We've done that many times already.

148
00:11:09,440 --> 00:11:18,620
But what happens if you want to store more than one string in a variable so you might have a selection

149
00:11:18,620 --> 00:11:19,970
of users, for example.

150
00:11:19,970 --> 00:11:21,890
So they might be just first names.

151
00:11:21,890 --> 00:11:29,060
So I want to have a variable that will store more than one string, which happens to be a first name

152
00:11:29,060 --> 00:11:30,250
in the same place.

153
00:11:30,740 --> 00:11:36,890
Well, I can do that simply by putting these characters in front of the variable name.

154
00:11:37,400 --> 00:11:42,730
This is now a slice of strings, which means I can put more than one thing into it.

155
00:11:43,220 --> 00:11:47,060
Well, how do you add something to a slice that's not actually not that hard?

156
00:11:47,360 --> 00:11:54,230
I can simply say my string, which I'm going to rename to my slice, we'll call it my slice, rename

157
00:11:54,230 --> 00:12:00,140
it here equals append to put on the end of the slice.

158
00:12:00,140 --> 00:12:03,210
The slice I want to append to which is my slice.

159
00:12:03,210 --> 00:12:06,680
It's the only thing I have here and some value so.

160
00:12:06,760 --> 00:12:12,080
My name there and now what happens if I try to print that slice out?

161
00:12:12,190 --> 00:12:13,640
Let's run this program and find out.

162
00:12:13,660 --> 00:12:16,570
Go run Mango and it prints out.

163
00:12:16,870 --> 00:12:19,990
See how it put the braces, the square brackets around it, Trevor.

164
00:12:20,140 --> 00:12:28,720
Well, let's put another value in the string, John, and now run it again and I'll clear the screen

165
00:12:28,720 --> 00:12:29,880
so we can see it more easily.

166
00:12:30,970 --> 00:12:33,070
Now, it has two values, Trevor and John.

167
00:12:33,310 --> 00:12:38,080
I can put a third value in there, Mary and run it again.

168
00:12:39,220 --> 00:12:39,880
Go Roumain.

169
00:12:39,880 --> 00:12:41,210
Go Trevor.

170
00:12:41,230 --> 00:12:42,110
John and Mary.

171
00:12:42,310 --> 00:12:48,010
So now I have a data type called a slice, and this happens to be a slice of strings where I can put

172
00:12:48,010 --> 00:12:48,880
multiple values.

173
00:12:49,300 --> 00:12:54,070
Well, what if I change this from a slice of strings to a slice of it?

174
00:12:54,520 --> 00:12:58,300
Well, first of all, I only get three errors, but I can put two.

175
00:13:00,100 --> 00:13:00,580
One.

176
00:13:02,710 --> 00:13:10,570
And three and now if I run this, I should see two, one, three in exactly the order that I added them

177
00:13:11,290 --> 00:13:12,790
to one three.

178
00:13:12,820 --> 00:13:13,520
That was great.

179
00:13:13,960 --> 00:13:21,970
Can I do anything to a slice while we can find out really easily type of my slice followed by Duck and

180
00:13:21,970 --> 00:13:26,270
then we have append a time to sign cap copy for four.

181
00:13:26,350 --> 00:13:28,150
We have all kinds of things we can do to it.

182
00:13:28,450 --> 00:13:29,120
Pretty line.

183
00:13:30,040 --> 00:13:31,370
What's this one sought.

184
00:13:31,390 --> 00:13:32,350
Let's try sorting it.

185
00:13:34,840 --> 00:13:36,670
Sort in my slice.

186
00:13:37,390 --> 00:13:38,660
Well, let's store that somewhere.

187
00:13:41,020 --> 00:13:44,440
Sorted is assigned sort since my slice.

188
00:13:44,740 --> 00:13:45,910
No, that's not what it does.

189
00:13:45,970 --> 00:13:46,740
Let's try it again.

190
00:13:47,140 --> 00:13:50,620
Let's just run it like this is it looks like it's actually going to sort the.

191
00:13:50,620 --> 00:13:52,010
It's in my slice.

192
00:13:53,020 --> 00:13:54,750
Let's run the program now.

193
00:13:54,790 --> 00:13:58,090
Last time we got two, one, three, the order we put them in.

194
00:13:58,120 --> 00:14:01,020
This time we get one, two, three.

195
00:14:01,030 --> 00:14:01,420
Look at that.

196
00:14:01,420 --> 00:14:04,270
I can sort a slice that works really well.

197
00:14:04,810 --> 00:14:06,910
So slices are extremely useful.

198
00:14:06,910 --> 00:14:12,030
And the great thing about them is you can declare it and then put as many values as you want in there.

199
00:14:13,210 --> 00:14:15,970
So that's an extremely helpful feature.

200
00:14:18,340 --> 00:14:24,070
Now, there are other ways of actually declaring slices, for example, I can use that shorthand, I

201
00:14:24,070 --> 00:14:33,310
can say, for example, numbers he's assigned and this time I'm going to go slice of it and then I will

202
00:14:33,310 --> 00:14:34,320
put some numbers in there.

203
00:14:34,330 --> 00:14:43,430
So one, two, three, four, five, six, seven, eight, nine, eight, eight, nine and 10.

204
00:14:44,080 --> 00:14:45,450
So now I have a slice.

205
00:14:45,460 --> 00:14:51,600
I've used the shorthand to actually declare a slice of INTs and then I pre populated it with ten values.

206
00:14:51,880 --> 00:15:01,690
So if I put that out, I can go log the print line numbers and reformat to make sure that my imports

207
00:15:01,690 --> 00:15:01,990
are right.

208
00:15:01,990 --> 00:15:02,470
They are.

209
00:15:02,590 --> 00:15:06,060
And that should print out the numbers from one to 10 with square brackets around it.

210
00:15:06,910 --> 00:15:07,660
And it does.

211
00:15:08,260 --> 00:15:12,790
What if I only want to say look at a range of that?

212
00:15:12,940 --> 00:15:13,660
Well, I can do that.

213
00:15:14,020 --> 00:15:15,490
Let's say I want to print the first three.

214
00:15:15,730 --> 00:15:24,880
I can say log print line and this time I want to go numbers, but only the first three and slices like

215
00:15:24,880 --> 00:15:26,950
arrays start counting at zero.

216
00:15:26,950 --> 00:15:29,110
So I can say from zero to two.

217
00:15:30,340 --> 00:15:35,710
And when I run the program this time I get just the first two numbers.

218
00:15:35,920 --> 00:15:41,320
I can go somewhere in the middle, I can go from six to eight or nine.

219
00:15:41,320 --> 00:15:42,070
I guess I typed nine.

220
00:15:42,080 --> 00:15:45,310
So we're going to go with nine, which should give me the last few bits.

221
00:15:46,730 --> 00:15:48,590
And it does seven, eight, nine.

222
00:15:49,730 --> 00:15:55,280
All right, so that works reasonably well and slices will be extremely helpful for you.

223
00:15:55,310 --> 00:15:59,490
And just as you can have a map of different types, we can have a slice of different types.

224
00:15:59,510 --> 00:16:07,070
So if I delete everything and go, my child, do use the shorthand again, names is equal to a slice

225
00:16:07,070 --> 00:16:08,090
of string.

226
00:16:08,540 --> 00:16:16,950
And then in curly brackets, I put in some strings, one seven fish cap.

227
00:16:17,270 --> 00:16:23,030
Now I've created a slice of strings and I can actually print that out, a catalog print line and I'll

228
00:16:23,030 --> 00:16:23,960
print out names.

229
00:16:25,010 --> 00:16:27,380
And if I run this, I'll clear the screen.

230
00:16:27,650 --> 00:16:28,670
Run the program.

231
00:16:29,090 --> 00:16:29,840
Away we go.

232
00:16:29,840 --> 00:16:36,710
One seven fish cat so slices you'll be using them all the time will be using them regularly as we go

233
00:16:36,710 --> 00:16:40,300
through this course and we start, for example, pulling information out of the database.

234
00:16:40,550 --> 00:16:45,560
Sometimes when you access one thing from a database, you want one thing from a database, but very

235
00:16:45,560 --> 00:16:51,940
often you want to say go to the user's table and get me all of the users that have the status of inactive.

236
00:16:52,190 --> 00:16:56,660
And what we're going to give back is a slice of users in that case.

237
00:16:56,750 --> 00:17:00,100
But we got a little bit to cover before we can get that far along.

238
00:17:00,710 --> 00:17:06,560
But for right now, we have two new data types at our disposal, maps and slices.
