1
00:00:01,060 --> 00:00:07,390
So we left off here last time we wrote a very simple hello world web application, everything is built

2
00:00:07,390 --> 00:00:13,390
into one function, the main function, and all it does is it doesn't matter what page you go to, it

3
00:00:13,390 --> 00:00:14,460
just returns.

4
00:00:15,040 --> 00:00:16,560
Hello, world to the screen.

5
00:00:16,690 --> 00:00:21,240
So let's make this a little more useful and we're going to do this really gradually.

6
00:00:21,310 --> 00:00:25,330
First of all, if you have the application running in your terminal window, you need to quit out of

7
00:00:25,330 --> 00:00:25,480
it.

8
00:00:25,630 --> 00:00:31,630
Control seat will stop it and then I'll hide my terminal and let's have a look at what we have here.

9
00:00:31,630 --> 00:00:32,080
So far.

10
00:00:32,090 --> 00:00:38,300
Right now, we have the main function, plus this handler function, which is built in.

11
00:00:38,320 --> 00:00:41,750
It has a built-In anonymous function with the does everything we want it to do.

12
00:00:42,460 --> 00:00:45,870
So this works, but it might be a little more useful.

13
00:00:45,880 --> 00:00:51,490
Let's pretend we're building a website that consists of two pages, so we're going to have the home

14
00:00:51,490 --> 00:00:51,940
page.

15
00:00:51,940 --> 00:00:57,790
So I'm going to create a new function called Func Home, and this is going to be a handler function.

16
00:00:58,090 --> 00:01:03,430
And this is something you need to remember in order for a function to respond to a request from a Web

17
00:01:03,430 --> 00:01:06,190
browser, it has to handle two parameters.

18
00:01:06,640 --> 00:01:14,050
A response writer, which I'm going to call w http response writer, and it has to handle a request

19
00:01:14,090 --> 00:01:18,850
r star, dot star, dot request.

20
00:01:19,720 --> 00:01:21,370
And then of course it has to have the Brace's.

21
00:01:21,370 --> 00:01:24,270
So there is a function that will actually handle requests.

22
00:01:24,310 --> 00:01:28,780
Right now it doesn't do anything, but it does in fact handle requests.

23
00:01:29,050 --> 00:01:34,120
And what I can do is instead of this handler func with an embedded function, I can delete that entirely

24
00:01:34,690 --> 00:01:38,800
and say I'm going to make a http dot handle func.

25
00:01:39,720 --> 00:01:45,660
And it's going to respond to the root level of my Web application, in other words, the homepage,

26
00:01:46,020 --> 00:01:48,410
and then it's going to call a function home.

27
00:01:49,020 --> 00:01:53,580
Well, Hung is actually this function up here and this doesn't do anything.

28
00:01:54,000 --> 00:02:04,830
But I can, of course, do my standard response and just say format F print F F print F W for my response

29
00:02:04,830 --> 00:02:06,260
writer and then just return.

30
00:02:07,080 --> 00:02:13,920
This is the home page and then I'm going to have a second function and this is going to be an about

31
00:02:13,920 --> 00:02:14,250
page.

32
00:02:14,250 --> 00:02:17,610
So I'm just going to duplicate this line just to save copying and pasting.

33
00:02:18,090 --> 00:02:24,450
And I'm going to say for any request that comes to about, I want to go to a function called the boat,

34
00:02:25,050 --> 00:02:26,150
which doesn't exist yet.

35
00:02:26,310 --> 00:02:31,540
But again, I can copy this, paste it here, change it to a boat.

36
00:02:31,800 --> 00:02:33,960
So now this is linked to this.

37
00:02:34,350 --> 00:02:36,630
And I'm going to say instead of this is the home page.

38
00:02:36,630 --> 00:02:38,430
This is the about page.

39
00:02:39,060 --> 00:02:39,770
This is clear.

40
00:02:39,960 --> 00:02:46,260
Now, I have really short lines in my main function that point to external functions, but we can make

41
00:02:46,260 --> 00:02:47,610
a little more complex than that.

42
00:02:47,610 --> 00:02:52,040
Instead of just saying this is the about page, let's have it return a computed function.

43
00:02:52,320 --> 00:02:54,480
So I'm going to have another function func.

44
00:02:54,870 --> 00:02:58,020
And this doesn't actually have it's not going to be a handler.

45
00:02:58,020 --> 00:03:02,550
It's just going to be a standard function that I can do anything with and I'll call it add values.

46
00:03:03,060 --> 00:03:08,520
And this is going to take two parameters, X and Y, both of which are integers.

47
00:03:08,520 --> 00:03:14,210
And it's going to return an integer, which is just not integer.

48
00:03:15,060 --> 00:03:17,220
And I'm going to do is return.

49
00:03:19,770 --> 00:03:22,490
X plus one there.

50
00:03:22,920 --> 00:03:27,960
So now this function isn't called yet, hence my ID, hopefully Graser to let me know that it's unused

51
00:03:28,440 --> 00:03:33,840
but it's going to take to values X and Y, it's going to add them and return that value.

52
00:03:33,870 --> 00:03:43,560
Now, I could have done this in two lines and said some is assigned the value of X plus Y and then return

53
00:03:43,560 --> 00:03:47,370
some instead or and you just get used to this.

54
00:03:47,370 --> 00:03:51,120
There's multiple ways of doing things in certain parts of go.

55
00:03:51,480 --> 00:03:55,530
I could declare a variable sum that's an ENT and then make some.

56
00:03:55,710 --> 00:03:57,770
I'm not creating and assigning the type here.

57
00:03:57,780 --> 00:03:58,800
This is the shorthand.

58
00:03:59,220 --> 00:04:02,170
I could just say some equals X plus Y and that works.

59
00:04:02,190 --> 00:04:03,020
All of those work.

60
00:04:03,300 --> 00:04:09,420
Of course I like to be concise, so the way I'm actually going to do it is to return X plus what.

61
00:04:10,540 --> 00:04:14,350
Great, now you've got to start thinking like a programmer.

62
00:04:14,740 --> 00:04:19,790
Are there any situations where this function might not work?

63
00:04:20,260 --> 00:04:26,080
What if I pass it X and Y, but one of those is not an integer?

64
00:04:26,500 --> 00:04:28,210
Well, actually, it wouldn't compile.

65
00:04:28,210 --> 00:04:35,470
But for the sake of argument, I probably want to say don't just return an ink type, return an integer.

66
00:04:35,470 --> 00:04:40,950
And if there's an error, return that which means I'm returning to types.

67
00:04:41,410 --> 00:04:44,370
So here I can't do the shorthand of X plus one.

68
00:04:44,380 --> 00:04:45,550
I need to add them together.

69
00:04:46,150 --> 00:04:53,480
So I could say if I was throwing an error, I would have to return my values so I would declare a variable.

70
00:04:53,500 --> 00:04:56,790
I'm actually not going to do this because it's impossible to throw an error here easily.

71
00:04:57,100 --> 00:05:06,570
I could say var some sum equals X plus Y and then return that and nil because there's no error.

72
00:05:06,670 --> 00:05:11,530
But we actually don't have returns some and nil and some is in it.

73
00:05:13,100 --> 00:05:15,430
Like I need more caffeine anyway.

74
00:05:15,430 --> 00:05:18,430
That's what I would normally do and I would check for an error here.

75
00:05:18,430 --> 00:05:22,030
I'm just adding to since I know they're going to be ense because it's a strongly typed language, I

76
00:05:22,030 --> 00:05:23,830
can't accept anything that's not an end.

77
00:05:24,130 --> 00:05:27,460
But I can return my value and an error if there isn't any.

78
00:05:27,520 --> 00:05:36,130
Of course, this is a really simple, straightforward function, so I'll just go return X plus Y and

79
00:05:36,130 --> 00:05:41,830
make this return just an int, but we'll create a third page in a minute where it's more possible to

80
00:05:41,830 --> 00:05:42,370
have an error.

81
00:05:42,490 --> 00:05:44,800
OK, so now I've got this function.

82
00:05:44,800 --> 00:05:46,470
I actually want to return it.

83
00:05:46,780 --> 00:05:52,210
So let's go up here and call it, let's say, my sum.

84
00:05:52,210 --> 00:05:59,770
And here the scope of this sum is only within this variable that's going to be equal to add values and

85
00:05:59,770 --> 00:06:02,600
I'll pass it to integers two and two.

86
00:06:03,760 --> 00:06:06,160
And I can't just not use this variable.

87
00:06:06,160 --> 00:06:06,850
I've created it.

88
00:06:06,850 --> 00:06:08,830
So Go says we've got that variable.

89
00:06:08,980 --> 00:06:11,510
I don't want variables hanging around that don't do anything.

90
00:06:11,770 --> 00:06:13,680
So if you're going to create it, you better use it.

91
00:06:13,990 --> 00:06:18,850
So let's change this instead of just returning a string here, let's cut that out there and format as

92
00:06:18,850 --> 00:06:27,970
print F and now we're going to say this is the about page and two plus two is and now I'm going to put

93
00:06:27,970 --> 00:06:31,750
it in there and remember first print F which I better spell right.

94
00:06:33,400 --> 00:06:39,670
S print F actually wants a placeholder and the placeholder for it is percent d if it was a string it

95
00:06:39,670 --> 00:06:44,380
would be percent s but it's a we'll learn more about that as we use it right now.

96
00:06:44,380 --> 00:06:47,950
I need to put it at integer value in there and I just calculated one.

97
00:06:47,950 --> 00:06:52,510
Am I calculated integer is something so I can put that in there and write it out.

98
00:06:52,840 --> 00:06:55,840
And this throws an error which I'm going to ignore right now.

99
00:06:55,840 --> 00:06:59,890
And remember, the shorthand for ignoring and ignoring something that's return by function is to use

100
00:06:59,890 --> 00:07:00,610
the underscore.

101
00:07:01,750 --> 00:07:04,300
So F productive actually returns to things.

102
00:07:04,480 --> 00:07:04,810
Right?

103
00:07:04,820 --> 00:07:07,290
So I'll ignore both of them.

104
00:07:08,110 --> 00:07:13,960
So now I've created a home handler, I've created an about handler and I have these which point to those

105
00:07:13,960 --> 00:07:17,460
functions and this function about actually calls add values.

106
00:07:18,370 --> 00:07:23,980
Now, here's another thing you have to get used to when a function name or a variable name, which will

107
00:07:23,980 --> 00:07:30,030
get to later on, begins with a capital letter, it means it's accessible outside a given package.

108
00:07:30,040 --> 00:07:33,700
It's not true for the main package because you can't call the main package from an external package,

109
00:07:34,270 --> 00:07:37,300
but you may as well get in the habit of doing things properly.

110
00:07:37,570 --> 00:07:42,550
I am never going to call this function from anywhere else, so I'm going to give it a lowercase name

111
00:07:42,850 --> 00:07:44,020
and that hides it.

112
00:07:44,320 --> 00:07:49,600
If you come from object oriented background, if you've done some programming like in Java or even for

113
00:07:49,600 --> 00:07:53,530
that matter, you can think of this as making the function private.

114
00:07:54,100 --> 00:07:58,120
The only places that this function can be called from are within this package.

115
00:07:58,330 --> 00:07:59,500
And the way that go does it.

116
00:07:59,500 --> 00:08:05,800
If it has an upper area uppercase letter for the first letter of the function name, then it's visible

117
00:08:05,800 --> 00:08:06,940
outside of the package.

118
00:08:07,180 --> 00:08:09,240
If it has a lowercase one, it is not.

119
00:08:09,310 --> 00:08:11,020
So I'm going to leave it like that right now.

120
00:08:11,530 --> 00:08:16,150
And we may also get into the habit of giving comments to things as well because they're quite helpful.

121
00:08:16,600 --> 00:08:21,430
A comment is something in a source file that is ignored by the compiler, and it's just there for people

122
00:08:21,430 --> 00:08:22,630
like you and me to read.

123
00:08:22,990 --> 00:08:27,490
And you can start you can write a function with two slashes and the convention in go.

124
00:08:27,520 --> 00:08:31,360
Is that the very first thing that should appear in the comment above?

125
00:08:31,360 --> 00:08:38,250
A function is the function name about is the about the page scandal.

126
00:08:38,860 --> 00:08:45,060
And the same thing with Home Home is the home page handler.

127
00:08:47,230 --> 00:08:52,600
Now, you don't have to give things comments, of course, but you probably should, even though this

128
00:08:52,600 --> 00:08:54,580
one's not going to be visible outside of the package.

129
00:08:54,910 --> 00:08:56,170
I'm going to give it a comment.

130
00:08:57,670 --> 00:09:05,620
Add values, adds to integers and returns the sum and then main, I'll just call.

131
00:09:05,620 --> 00:09:08,350
This is the main.

132
00:09:10,670 --> 00:09:16,070
Application of the entry point, in other words, OK, so there we are.

133
00:09:16,250 --> 00:09:17,480
Now I've got this all built.

134
00:09:17,720 --> 00:09:19,880
Let's compile it and see what happens.

135
00:09:19,910 --> 00:09:24,410
So all I do is open my terminal and go run the main domain.

136
00:09:25,240 --> 00:09:25,570
Go.

137
00:09:25,640 --> 00:09:26,060
That's it.

138
00:09:26,630 --> 00:09:27,470
So it starts.

139
00:09:27,680 --> 00:09:28,820
It seems to be running.

140
00:09:29,060 --> 00:09:32,150
Nothing strange is happening, but I'm going to make it even even simpler.

141
00:09:32,540 --> 00:09:34,280
I'm going to stop this and come down here.

142
00:09:34,280 --> 00:09:41,090
And just before I call my HDB, listen and serve, I'm going to write to the console and I'm going to

143
00:09:41,090 --> 00:09:42,140
do that like this.

144
00:09:42,920 --> 00:09:47,300
I'm going to come to the top just after my import statements and I'm going to say, well, the port

145
00:09:47,390 --> 00:09:50,890
is something I might want to change and I might use it more than one place.

146
00:09:51,140 --> 00:09:57,200
So rather than just putting Port 80, 80 like this, I'm going to create a variable called Port No.

147
00:09:59,420 --> 00:10:03,530
Which doesn't exist yet, and I could declare it inside the main function if I wanted to, but I might

148
00:10:03,530 --> 00:10:04,910
want to use it another function.

149
00:10:04,910 --> 00:10:13,400
So to do that, I'm going to change where I declare this variable and I'm going to say var porked number

150
00:10:13,430 --> 00:10:15,960
equals Colen eighty eight.

151
00:10:16,490 --> 00:10:20,700
And now that I've done that, I can actually use that where I need to.

152
00:10:20,960 --> 00:10:24,900
So I've got my number now and I can use that down here and I already have.

153
00:10:24,920 --> 00:10:25,610
So that's great.

154
00:10:25,640 --> 00:10:28,100
So the simple thing I can do here is say Vanport.

155
00:10:28,100 --> 00:10:30,530
No, I can actually get better than that.

156
00:10:30,560 --> 00:10:34,850
I never want this number to be changed by another part of the application.

157
00:10:35,180 --> 00:10:39,440
So I can say instead of VAR, which is a variable which can you can just overwrite it with a different

158
00:10:39,440 --> 00:10:40,130
string value.

159
00:10:40,550 --> 00:10:41,750
I can call it a constant.

160
00:10:42,110 --> 00:10:47,280
And the nice thing about a constant is it can't be changed by anything else in the application.

161
00:10:48,020 --> 00:10:53,690
So now that I have my number specified, I can come down here and I want to write to the console and

162
00:10:53,690 --> 00:10:57,350
I'm just going to say format, print, line.

163
00:10:59,190 --> 00:11:01,700
And then again in here, I want to use that part number.

164
00:11:01,700 --> 00:11:04,220
So I'm going to use format as a print F.

165
00:11:07,490 --> 00:11:15,140
Starting, not staring, starting application on port, and this is a placeholder, but it's a string

166
00:11:15,410 --> 00:11:18,610
and it's not an int, which is the only thing we've used so far.

167
00:11:19,340 --> 00:11:21,230
So Percent s is the placeholder.

168
00:11:21,560 --> 00:11:24,610
And now I need to say, what do I want to replace that placeholder with?

169
00:11:24,620 --> 00:11:25,930
I want to replace it with Port.

170
00:11:25,940 --> 00:11:26,220
No.

171
00:11:26,810 --> 00:11:27,260
All right.

172
00:11:27,860 --> 00:11:29,710
So let's run the application again.

173
00:11:29,720 --> 00:11:34,640
And now when I start it up, it's going to say starting application on Port 80 80.

174
00:11:34,670 --> 00:11:39,070
So let's go over to my Web browser and refresh this page.

175
00:11:39,560 --> 00:11:41,030
And this is the home page.

176
00:11:41,060 --> 00:11:47,270
So when I get this is the home page up here, that's because I didn't specify anything after the URL.

177
00:11:47,270 --> 00:11:49,370
If I put a slash there, I should get the same thing.

178
00:11:50,210 --> 00:11:51,320
This is the home page.

179
00:11:51,680 --> 00:11:57,620
And down here it says in these handlers, aha, if it has nothing or a slash, go to home.

180
00:11:57,920 --> 00:12:01,250
And the home function just says this is the home page.

181
00:12:02,360 --> 00:12:03,740
Let's try the about page.

182
00:12:04,190 --> 00:12:12,230
So here back in my Web browser about this is the about page and two plus two is for exactly what I wanted.

183
00:12:12,230 --> 00:12:13,150
And what did it do.

184
00:12:13,160 --> 00:12:17,240
It said, well when you made the request, it wasn't just something to ignore that.

185
00:12:17,660 --> 00:12:19,670
And under here we have slash about.

186
00:12:19,820 --> 00:12:26,810
So now return whatever the about function gives us and the about function calls this function add values

187
00:12:27,170 --> 00:12:33,530
adds two plus to the parameters I specified here and stores that in the variable sum, which is an int

188
00:12:33,530 --> 00:12:38,090
because we use the shorthand assignment, this returns and therefore this is an Int.

189
00:12:38,840 --> 00:12:44,720
And down here I just say print to the response rate or w this computed statement.

190
00:12:44,840 --> 00:12:46,910
This is the which I spelled wrong.

191
00:12:46,910 --> 00:12:47,990
So I means we'll fix that.

192
00:12:48,260 --> 00:12:55,910
This is the about page and two plus two is percent D replace percent D with some and two plus two is

193
00:12:55,910 --> 00:12:56,390
four.

194
00:12:56,900 --> 00:12:57,590
So there you go.

195
00:12:57,980 --> 00:13:02,810
Now we can handle two pages but of course we can handle many pages, as many pages as we want to.

196
00:13:03,200 --> 00:13:05,800
But this is sufficient for our purposes for the moment.

197
00:13:06,230 --> 00:13:07,350
So let's stop now.

198
00:13:07,410 --> 00:13:09,290
We'll pick it up in the next lecture.
