1
00:00:01,230 --> 00:00:07,470
OK, so I'm still in our main Dongo, but I've deleted everything except what you see on the screen

2
00:00:07,470 --> 00:00:12,840
here, I just have a package declaration and empty brain function and I've left say something that function

3
00:00:12,840 --> 00:00:13,830
we created alone.

4
00:00:14,130 --> 00:00:18,720
And I want to talk about eventually I'm going to get to type's instructs, but I want to talk about

5
00:00:18,720 --> 00:00:21,170
variables and functions just a little bit more.

6
00:00:21,600 --> 00:00:27,780
And I want to draw your attention to new ways to initialize variables and alternate ways to initialize

7
00:00:27,780 --> 00:00:31,480
variables that are similar to the one we saw last time around.

8
00:00:31,800 --> 00:00:34,260
So in my main function, I have nothing.

9
00:00:34,260 --> 00:00:39,210
And above that I'm going to declare a variable outside of the main function, which will give me an

10
00:00:39,210 --> 00:00:42,530
opportunity to talk about scope and variable shadowing.

11
00:00:43,350 --> 00:00:46,860
So I'm going to create a variable var s string.

12
00:00:47,110 --> 00:00:49,770
OK, that's one way to declare a variable.

13
00:00:49,770 --> 00:00:50,640
That's what we did before.

14
00:00:50,670 --> 00:00:54,030
There's another way to do it, to declare it and initialize it at the same time.

15
00:00:54,030 --> 00:00:56,280
And I'll call this seven.

16
00:00:56,700 --> 00:01:03,180
So I've declared a variable of type S that the go compiler is smart enough to say I'm going to infer

17
00:01:03,180 --> 00:01:05,550
the type based upon what you're setting it equal to.

18
00:01:05,580 --> 00:01:08,190
This is a string, therefore X is now a string.

19
00:01:08,580 --> 00:01:18,090
And in my main function, the very first thing I'm going to do is say log print line and I will print

20
00:01:18,090 --> 00:01:19,140
that variable X.

21
00:01:19,590 --> 00:01:24,410
So this variable is declared outside of this function.

22
00:01:24,750 --> 00:01:29,430
Therefore this variable is available to every function in this package.

23
00:01:29,550 --> 00:01:32,420
OK, so that is the scope of this variable.

24
00:01:32,430 --> 00:01:38,670
When I declare a variable in here, var that's two equals six.

25
00:01:39,930 --> 00:01:42,800
This is only available inside the main function.

26
00:01:42,810 --> 00:01:46,890
Say something, doesn't know anything about it, but you notice something here that's a bit of a problem.

27
00:01:46,920 --> 00:01:50,070
OK, well maybe don't, let's just give it an example.

28
00:01:50,070 --> 00:01:52,890
So I print out just to make this work.

29
00:01:52,890 --> 00:01:55,260
Let's get rid of have this log print line s.

30
00:01:55,530 --> 00:02:01,440
Let's duplicate that line log print line S.

31
00:02:01,680 --> 00:02:04,950
S two is comma and then two.

32
00:02:06,840 --> 00:02:14,640
And here I'll put S is and then s now you notice here that I've passed two strings to this one that

33
00:02:14,640 --> 00:02:19,560
I've written by hand by putting quotes around it and one that is a variable and I've done the same thing

34
00:02:19,560 --> 00:02:19,800
here.

35
00:02:19,800 --> 00:02:23,880
Logged up print line is what's called a very ADIC function that will be seeing a little bit later on.

36
00:02:24,150 --> 00:02:28,350
It means you can take one or more parameters or even know parameters.

37
00:02:28,380 --> 00:02:34,200
OK, so all this is going to have all this going to happen right now is I'm going to run the main program.

38
00:02:34,290 --> 00:02:36,090
I'm going to say go run bingo.

39
00:02:36,390 --> 00:02:43,620
And it will run this program and say in the main function, assign a sign, the value of six, the characters

40
00:02:43,620 --> 00:02:50,160
six to the string variable to then print whatever the value of S's and print whatever the value of S2

41
00:02:50,160 --> 00:02:50,370
is.

42
00:02:50,400 --> 00:02:51,040
Let's run that.

43
00:02:51,630 --> 00:02:55,650
So let me say this and go run mango.

44
00:02:57,260 --> 00:03:04,530
Seven to six, perfect, that's exactly what I expected to happen, but now let's add a call to this

45
00:03:04,530 --> 00:03:05,430
function down here.

46
00:03:05,430 --> 00:03:11,300
Say something, say something, and I'm going to pass it x, x, x, OK?

47
00:03:11,340 --> 00:03:13,620
Just because I know that's very different than anything I have so far.

48
00:03:13,650 --> 00:03:14,710
So what do you think is going to happen?

49
00:03:15,270 --> 00:03:17,670
I'm going to come down here and say, say something.

50
00:03:18,060 --> 00:03:20,790
And it takes a parameter called s.

51
00:03:21,090 --> 00:03:25,860
I already have an S up here and it's set the value of seven and I have an S down here.

52
00:03:26,160 --> 00:03:28,080
Which s does this refer to.

53
00:03:28,080 --> 00:03:30,720
Does it refer to this s or this.

54
00:03:30,720 --> 00:03:36,990
S It's not clear and that's a clear example of why you need to be careful with your variable names.

55
00:03:37,030 --> 00:03:38,310
Let's run it and see what happens.

56
00:03:40,500 --> 00:03:51,330
So it says, don't forget to print that out log print line tests, and in front of that, I'll put S

57
00:03:51,570 --> 00:03:55,830
from the say something.

58
00:03:58,370 --> 00:04:02,910
Funk, is this safe that cleared the screen?

59
00:04:02,930 --> 00:04:03,560
Run it again.

60
00:04:03,590 --> 00:04:09,010
Let's find out which s that actually is s from the say something funky is X, X, X.

61
00:04:09,380 --> 00:04:17,300
So here you have this variable s, which you might think is this value from up here being overwritten

62
00:04:17,300 --> 00:04:23,270
because we used a the same variable name for the parameter and we can fix that just by saying as three.

63
00:04:24,910 --> 00:04:30,940
And we run out of turns three and now it's run it, so save that and see what happens.

64
00:04:32,260 --> 00:04:34,090
Clear the screen, go run Mingo.

65
00:04:34,120 --> 00:04:35,020
What should happen?

66
00:04:35,530 --> 00:04:42,040
Well, it should say, first of all, print s, which we know is a package level variable with a value

67
00:04:42,040 --> 00:04:48,940
of seven spelled out print s to which I assigned to value to right here so that you print out six spelled

68
00:04:48,940 --> 00:04:56,680
out then I pass x x x to say something which calls that variable s three which should, what should

69
00:04:56,680 --> 00:05:01,990
happen here is it should say s from the say something func is s from up here seven.

70
00:05:01,990 --> 00:05:03,790
So I should see seven, six, seven.

71
00:05:04,450 --> 00:05:04,990
Let's run it.

72
00:05:05,860 --> 00:05:07,640
Seven, six, seven.

73
00:05:07,750 --> 00:05:08,380
Perfect.

74
00:05:08,680 --> 00:05:09,920
That's ideal.

75
00:05:10,540 --> 00:05:14,250
All right, so there's one example of of things you need to pay attention to.

76
00:05:14,260 --> 00:05:16,630
There are other ways that variable shadowing can happen.

77
00:05:16,900 --> 00:05:23,560
I might, for example, come up in the main function, declare a new variable called s using a different

78
00:05:23,560 --> 00:05:24,060
syntax.

79
00:05:24,070 --> 00:05:26,590
So far all we've seen is this vast string.

80
00:05:27,100 --> 00:05:27,790
And I can do that.

81
00:05:28,000 --> 00:05:29,530
Absolutely, I can do that.

82
00:05:29,890 --> 00:05:35,860
But as far as this function is concerned, s every time I refer to it is going to be whatever of value

83
00:05:35,860 --> 00:05:37,180
I give it within this function.

84
00:05:37,180 --> 00:05:39,000
It's never going to pay attention to this one.

85
00:05:39,610 --> 00:05:44,080
So instead, let's let's look at a different way of initializing a variable S.

86
00:05:45,100 --> 00:05:47,640
S and now I'm not going to use the equals sign.

87
00:05:47,650 --> 00:05:51,970
I'm going to use the column equal sine and I'm going to give that the value of eight.

88
00:05:53,350 --> 00:05:56,050
Eventually I'm going to get confused as to which numbers I'm using.

89
00:05:56,050 --> 00:05:58,410
But as long as I keep it to five or less, I should be OK.

90
00:05:58,690 --> 00:06:00,810
So I've created a new variable here in a different way.

91
00:06:00,820 --> 00:06:05,830
And again, this is because the compiler is very smart, because I didn't use an equal sign.

92
00:06:06,370 --> 00:06:11,620
Actually, what it's doing is saying take this variable, whatever, whatever it is, give it the type

93
00:06:11,620 --> 00:06:12,880
of whatever is over here.

94
00:06:12,880 --> 00:06:13,590
That's a string.

95
00:06:14,020 --> 00:06:18,670
So now I have two variables, again, with the same name and it's easy to get confused.

96
00:06:18,850 --> 00:06:23,470
And when you're writing code, you might think when you use the letter s that you're referring to this,

97
00:06:23,770 --> 00:06:25,780
but in fact, you're referring to this.

98
00:06:25,780 --> 00:06:26,590
So don't do that.

99
00:06:26,620 --> 00:06:28,630
Be very careful with your variable names.

100
00:06:28,660 --> 00:06:30,940
OK, but that's one way of getting it.

101
00:06:31,300 --> 00:06:31,750
All right.

102
00:06:31,870 --> 00:06:33,510
Let's move on to a new topic.

103
00:06:34,090 --> 00:06:38,530
Sometimes the primitive variables are not enough to do what you want them to do.

104
00:06:39,130 --> 00:06:46,270
So, for example, let's say that I am thinking about putting a person in a database and there are certain

105
00:06:46,270 --> 00:06:48,430
kinds of information I want to save about that person.

106
00:06:48,760 --> 00:06:54,220
So I want to say first name, last name, phone number, address, whatever it may be, there might

107
00:06:54,220 --> 00:06:58,570
be 30 different things I'm going to store about a particular individual, but I store them in a database.

108
00:06:59,110 --> 00:07:11,350
Now, I could do that if I wanted to by saying via first name string var last name string ver phone

109
00:07:11,350 --> 00:07:18,310
number string var age int there birth date.

110
00:07:18,820 --> 00:07:21,010
And here's a new type that you're not familiar with yet.

111
00:07:21,010 --> 00:07:24,850
But I, I want, I want to store a date, I could store a string, but that's really not a I mean a

112
00:07:24,850 --> 00:07:25,570
string is a string.

113
00:07:25,570 --> 00:07:31,930
It can be anything I want to take advantage of goes strong typing and ensure that when I saw a date

114
00:07:32,050 --> 00:07:35,950
I'm actually storing a date so I could use a built in package.

115
00:07:35,950 --> 00:07:38,590
That's part of the gold standard library called Time.

116
00:07:39,190 --> 00:07:42,130
And the time type I want to store is actually time.

117
00:07:42,670 --> 00:07:46,600
And in that I can store actually a time or a date or a date in a time.

118
00:07:47,020 --> 00:07:47,980
So I'm going to use that.

119
00:07:47,980 --> 00:07:50,230
I could do that and say, OK, there it is.

120
00:07:50,500 --> 00:07:55,750
And every time I wanted to do something with a person, I could pass along one, two, three, four,

121
00:07:55,750 --> 00:07:57,460
five parameters in functions.

122
00:07:57,800 --> 00:08:02,860
What if that gets to twenty function twenty, twenty bits of information I want to pass along.

123
00:08:02,860 --> 00:08:08,560
Suddenly I have functions with twenty parameters that's going to take forever to type it out and it's

124
00:08:08,560 --> 00:08:12,460
going to look ugly on the screen and you'll be scroll scrolling left and right and it gets extremely

125
00:08:12,460 --> 00:08:13,030
confusing.

126
00:08:13,210 --> 00:08:15,730
So we don't do it that way and go.

127
00:08:15,730 --> 00:08:23,260
What we do instead is we define a type and I can call that type user and it is a struct which you may

128
00:08:23,260 --> 00:08:26,530
never have seen before unless you've programmed in C or a similar language.

129
00:08:27,070 --> 00:08:30,850
But a struct is, as its name implies, just a structure.

130
00:08:31,030 --> 00:08:38,740
And I'm defining that by putting curly braces here and saying this struct contains all kinds of bits

131
00:08:38,740 --> 00:08:39,490
of information.

132
00:08:39,790 --> 00:08:47,500
And those are going to be I'm going to call it first name, which is a string last name, which is a

133
00:08:47,500 --> 00:08:47,950
string.

134
00:08:50,290 --> 00:08:55,360
Phone number, which is a string, and I'll tell you why I'm capitalizing that in a moment, but let

135
00:08:55,360 --> 00:09:01,420
me finish typing first age, which is an entry and birth date, which is time.

136
00:09:01,660 --> 00:09:02,080
Time.

137
00:09:04,760 --> 00:09:09,070
And when I say this, it formatted nicely and I can get rid of this, I don't need that.

138
00:09:09,080 --> 00:09:17,450
Instead, instead of having one, two, three, four, five variables, I have one type of variable.

139
00:09:17,480 --> 00:09:18,550
This is not a variable.

140
00:09:18,590 --> 00:09:21,470
This is a type which can be used as a variable.

141
00:09:21,860 --> 00:09:24,230
So let's get rid of this stuff because we don't need it right now.

142
00:09:25,100 --> 00:09:26,240
And let's get rid of this.

143
00:09:27,170 --> 00:09:29,330
Let's get rid of this function altogether, OK?

144
00:09:30,080 --> 00:09:31,100
And save this.

145
00:09:31,490 --> 00:09:33,800
And now I have a main program that does nothing.

146
00:09:33,800 --> 00:09:34,810
I actually don't need that either.

147
00:09:34,820 --> 00:09:35,420
Let me get rid of it.

148
00:09:37,190 --> 00:09:39,710
So I have a main program that does nothing.

149
00:09:39,710 --> 00:09:44,060
I have imported time because I'm using in this struct, but I'm not doing anything yet.

150
00:09:44,090 --> 00:09:47,090
I have this this type defined, but I'm not even using it.

151
00:09:47,600 --> 00:09:49,430
Let me create a user.

152
00:09:49,670 --> 00:09:56,060
So I'm going to do that in my main function by specifying a variable name user and I'm going to use

153
00:09:56,060 --> 00:09:59,690
the shorthand is equal to what is it equal to.

154
00:09:59,900 --> 00:10:07,700
It is equal to a user, which is this one, and it has certain bits of information and the informations

155
00:10:07,700 --> 00:10:18,620
are first name, which I'm going to give the value of Trever and then a comma, last name and a colon

156
00:10:19,070 --> 00:10:21,680
solla and that's enough.

157
00:10:21,680 --> 00:10:23,190
I'm not going to bother populating the rest of it.

158
00:10:23,210 --> 00:10:26,000
The point is, I can specify all of those now.

159
00:10:26,000 --> 00:10:28,310
I've not actually done anything with that variable.

160
00:10:28,310 --> 00:10:32,390
So again, the compiler saying how you declared a variable, but you're not doing anything with it.

161
00:10:32,390 --> 00:10:33,260
That's bad.

162
00:10:34,040 --> 00:10:39,710
And you notice all I did was say this user is of type user with a capital you up here.

163
00:10:40,100 --> 00:10:43,370
And it has these two things filled out, a first name and last name.

164
00:10:43,370 --> 00:10:45,020
I didn't bother filling out the rest of them.

165
00:10:45,020 --> 00:10:46,640
I can, but I'm not going to right now.

166
00:10:47,270 --> 00:10:49,800
How do I access information from that user?

167
00:10:49,820 --> 00:10:51,470
Well, let's use our log function again.

168
00:10:51,770 --> 00:10:54,050
Log print line.

169
00:10:55,220 --> 00:10:56,090
And I'm in that.

170
00:10:56,090 --> 00:11:01,640
I'm going to print user dot first name and that's how I refer to it.

171
00:11:02,000 --> 00:11:06,380
This user has a member or a field called First Name.

172
00:11:06,380 --> 00:11:07,270
I'm going to print that out.

173
00:11:07,490 --> 00:11:13,930
Let's run this, save it, open our terminal and run it and see what happens.

174
00:11:14,030 --> 00:11:15,920
Go run, Mingo.

175
00:11:17,060 --> 00:11:18,290
And it prints up my first name.

176
00:11:18,770 --> 00:11:25,130
What I can also come in here and say print at the last name to use your last name, save it.

177
00:11:25,610 --> 00:11:26,480
Cleared the screen.

178
00:11:26,990 --> 00:11:27,380
Run it.

179
00:11:28,290 --> 00:11:31,790
OK, well what happens if I try to print my birthdate, which I didn't specify.

180
00:11:32,780 --> 00:11:33,560
Let's try that.

181
00:11:36,890 --> 00:11:44,180
I'll put the birth date just for a label, comma, user, the birth date.

182
00:11:44,840 --> 00:11:46,690
Save it and run it.

183
00:11:47,840 --> 00:11:52,790
And as you might expect, because we didn't initialize birth date up here, we didn't give it a value

184
00:11:52,790 --> 00:11:53,200
here.

185
00:11:54,260 --> 00:11:58,400
It actually prints out the year one January the 1st midnight.

186
00:11:58,430 --> 00:11:58,820
All right.

187
00:11:58,820 --> 00:12:04,360
Well, that that actually is pretty straightforward and it's a really useful way to group things together.

188
00:12:04,400 --> 00:12:13,640
So I did also mention to you that I would tell you why I actually put let me say this, why I put these

189
00:12:14,480 --> 00:12:15,680
things with capital letters.

190
00:12:15,980 --> 00:12:23,360
I put them with capital letters because I knew I might need this user type to be available to another

191
00:12:23,360 --> 00:12:23,900
package.

192
00:12:24,320 --> 00:12:27,410
And go is not an object oriented programming language.

193
00:12:27,410 --> 00:12:34,250
If you're familiar with with objects dealing or object oriented languages like Java or Kotlin or to

194
00:12:34,250 --> 00:12:39,140
a certain extent, then you know what you can do in an object oriented language.

195
00:12:39,140 --> 00:12:48,890
You can say here is a function like I do a new function down here, func, whatever, and it doesn't

196
00:12:48,890 --> 00:12:50,240
do anything and I'm not going to fill it out.

197
00:12:50,690 --> 00:12:53,300
I can make this public, private or protect it.

198
00:12:53,300 --> 00:12:54,140
If it's public.

199
00:12:54,350 --> 00:13:02,150
I can refer to that function from outside of the current package in Java, for example, or if it's

200
00:13:02,600 --> 00:13:06,500
a private, you can't even see it outside of that package.

201
00:13:06,500 --> 00:13:11,000
So it's a way to keep bits of your program that you don't want exposed to other parts of your program

202
00:13:11,120 --> 00:13:12,050
to keep that private.

203
00:13:12,050 --> 00:13:15,440
Wigo doesn't have that functionality using object oriented syntax.

204
00:13:15,440 --> 00:13:22,100
Instead, if I declare a front like this, it's only available within this package, my main package.

205
00:13:22,370 --> 00:13:26,900
But if I declare it like this with a capital letter, it's visible outside of that package.

206
00:13:26,900 --> 00:13:31,640
And you can see that by looking at I see that in practice with another package, by looking at the long

207
00:13:31,650 --> 00:13:38,240
package, notice that every single thing here or every single variable here, you know that this one

208
00:13:38,240 --> 00:13:47,750
is a function fatele f it's visible and it starts with a capital letter in the same way this L date,

209
00:13:47,960 --> 00:13:53,150
which is the name of an int, it actually is visible as well, and it starts with a capital letter.

210
00:13:54,410 --> 00:14:05,540
If I have a variable declared in this package, if I put for example, var special string, that's a

211
00:14:05,540 --> 00:14:09,650
private variable only available to this particular package mean.

212
00:14:09,950 --> 00:14:15,290
But if I gave it a capital letter and this isn't true for the main package, but if you have a capital

213
00:14:15,290 --> 00:14:19,490
letter for the name of a variable inside a package, it's visible outside of that package.

214
00:14:20,060 --> 00:14:20,390
All right.

215
00:14:20,390 --> 00:14:22,790
That's why I put things the way that I did.

216
00:14:23,540 --> 00:14:25,760
So I get rid of these functions.

217
00:14:25,760 --> 00:14:26,510
I'm not using it.

218
00:14:28,130 --> 00:14:32,090
I've got a user type that has members and I now have access to them.

219
00:14:32,090 --> 00:14:39,860
If I want to give more information to this, I can simply say the phone number and it's a string so

220
00:14:39,860 --> 00:14:47,750
I could put one, whatever it is, five five five five five five, one, two and two.

221
00:14:48,290 --> 00:14:49,370
There's my phone number now.

222
00:14:49,370 --> 00:14:50,510
I've populated that.

223
00:14:51,590 --> 00:14:52,730
I do have an error here.

224
00:14:54,750 --> 00:14:56,430
You need to end this with a comma.

225
00:14:58,150 --> 00:15:05,920
So that is type that is variable shadowing life seems to be good so far in the next lecture, we're

226
00:15:05,920 --> 00:15:10,960
going to move on and we'll start looking at other data structures, maps and slices in particular.
