1
00:00:01,400 --> 00:00:07,310
So we left off last time with a simple Hello World program, and this time I want to make another hello

2
00:00:07,310 --> 00:00:11,840
world program, but I want to make it a little more complex this time.

3
00:00:11,840 --> 00:00:18,710
Instead of just printing Hello World to the screen, I want to create a Web application that listens

4
00:00:18,710 --> 00:00:25,250
on a particular part so I can hit that application with my favorite web browser and see Hello World

5
00:00:25,250 --> 00:00:26,730
printed to the browser window.

6
00:00:27,380 --> 00:00:33,200
So that is actually pretty simple and it doesn't sound that interesting, but it actually is a lot more

7
00:00:33,200 --> 00:00:34,190
interesting than you think.

8
00:00:34,190 --> 00:00:35,630
And you'll see why shortly.

9
00:00:36,140 --> 00:00:40,400
So I'm back in my Hello World program and I'm just going to modify this one.

10
00:00:40,760 --> 00:00:45,900
And this time, instead of just printing Hello World to the screen, I'm going to comment that oath,

11
00:00:46,910 --> 00:00:53,270
which automatically makes my idy remove that from FMT format, function or package that I don't need

12
00:00:53,270 --> 00:00:53,680
anymore.

13
00:00:53,960 --> 00:01:00,260
And this time I'm going to create a new function within my main function.

14
00:01:00,350 --> 00:01:02,080
And it's you'll see how it works here.

15
00:01:02,480 --> 00:01:08,230
So we're going to use a built in package that's part of the standard library for God called HTP.

16
00:01:08,390 --> 00:01:12,400
And as the name implies, it's one that deals with the web.

17
00:01:12,410 --> 00:01:14,450
So http dot.

18
00:01:15,200 --> 00:01:20,480
And then I get my idy giving me all of my things that I can possibly use from here.

19
00:01:20,480 --> 00:01:24,140
And you can see there's all kinds of things that are related to the Web.

20
00:01:24,380 --> 00:01:29,960
And that's not surprising because God was built from the ground up to be aware of the web and to be

21
00:01:29,960 --> 00:01:31,890
able to deal with it in a meaningful fashion.

22
00:01:32,330 --> 00:01:36,500
So I'm looking for one that starts with the letter H and it's called Handler Thunk.

23
00:01:36,830 --> 00:01:37,440
There it is.

24
00:01:37,910 --> 00:01:42,250
So that's part of the net HDB package, which I can see right here.

25
00:01:42,260 --> 00:01:47,360
So when I hit return to choose that, it should automatically import net HTTP.

26
00:01:47,360 --> 00:01:48,980
If it doesn't, you need to go type that in.

27
00:01:49,550 --> 00:01:54,030
Now this requires some arguments and the arguments it requires are pretty straightforward.

28
00:01:54,050 --> 00:01:59,720
The first thing it wants and we can see when I roll my mouse over it saying it needs a couple of things.

29
00:01:59,720 --> 00:02:04,060
It needs well to start with, I'm going to give it the pat name.

30
00:02:04,310 --> 00:02:10,090
That's the u r l, the uniform resource locator that I want to listen to.

31
00:02:10,100 --> 00:02:16,820
So when people go to my Web application and don't specify anything after the name of the host, here's

32
00:02:16,820 --> 00:02:18,050
what I want to give back to them.

33
00:02:18,500 --> 00:02:24,500
And what I'm going to give them is I'm actually going to inline a function here func and it's going

34
00:02:24,500 --> 00:02:29,030
to have W which is the HTTP response writer.

35
00:02:30,140 --> 00:02:34,700
This is the very first thing that you need to have because this is a handler function for the HTP.

36
00:02:35,090 --> 00:02:38,210
I need to be able to write responses to the user.

37
00:02:38,600 --> 00:02:40,490
So that's what the response writer does.

38
00:02:40,490 --> 00:02:43,940
And I've just called that w you can call it whatever you want to, you can call it yellow if you want

39
00:02:43,940 --> 00:02:45,020
to, but it's a writer.

40
00:02:45,020 --> 00:02:51,390
So I'm going to put W4 response writer and then I need the request and request starts with a letter.

41
00:02:51,590 --> 00:02:57,560
So I'm going to call my variable R this is my parameter for this function and it's a request now.

42
00:02:57,560 --> 00:02:59,870
It's not an EDP request.

43
00:03:00,440 --> 00:03:05,090
It's actually and this may be the very first time you've ever seen this in your entire programming career.

44
00:03:05,420 --> 00:03:09,770
It's a pointer and I can call it a pointer by putting a star in front of it.

45
00:03:10,070 --> 00:03:15,140
Now a pointer is nothing more than an address in memory where some value is stored.

46
00:03:15,590 --> 00:03:21,230
And the reason I have to use a pointer here is because the handler func requires the request to be a

47
00:03:21,230 --> 00:03:22,460
pointer to a request.

48
00:03:22,490 --> 00:03:28,400
So the request is what the Web server or web browser that's hitting my application sends.

49
00:03:28,400 --> 00:03:34,820
It sends a request and then I process that request, whatever it may be, and send back a response.

50
00:03:34,820 --> 00:03:39,620
And every HTTP handler function has to deal with responses and requests.

51
00:03:40,040 --> 00:03:42,970
So I have my my function partly written here.

52
00:03:43,010 --> 00:03:48,680
Let's get back between these two parentheses and actually do the body of the function and the body is

53
00:03:48,680 --> 00:03:53,540
going to be nothing more than I don't know why I have the letter in there.

54
00:03:53,540 --> 00:03:55,160
There should be no erm try that again.

55
00:03:55,160 --> 00:03:55,880
Handle func.

56
00:03:56,840 --> 00:04:01,910
I want to return something and what I want to return is just font which we saw before.

57
00:04:02,300 --> 00:04:03,020
Print line.

58
00:04:04,250 --> 00:04:05,600
Hello world.

59
00:04:06,350 --> 00:04:08,990
OK, so what reimported FMT for me.

60
00:04:08,990 --> 00:04:12,080
The format function and that's all that I have.

61
00:04:12,890 --> 00:04:15,710
So that is pretty straightforward.

62
00:04:15,950 --> 00:04:20,130
But I don't actually want to print a line here because printing a line is just going to print it to

63
00:04:20,150 --> 00:04:20,680
the console.

64
00:04:20,690 --> 00:04:21,920
So let's change that a little bit.

65
00:04:21,920 --> 00:04:29,450
Instead of using print line, let's use F print F and F print F actually requires a response writer

66
00:04:29,450 --> 00:04:30,110
to write to.

67
00:04:30,110 --> 00:04:36,350
Well I have one right here, the W, so let's put it W in there and now everything seems OK.

68
00:04:36,380 --> 00:04:40,010
The only danger is you notice there's a little squiggly line under this.

69
00:04:40,010 --> 00:04:43,520
It might be different in visual studio code but it's probably there in some format.

70
00:04:44,180 --> 00:04:47,530
If I roll over it, it says this is an unhandled error.

71
00:04:48,170 --> 00:04:53,420
Well, one of the cool things about the standard library is I can actually go and look at this actual

72
00:04:53,420 --> 00:04:57,290
built in function within the the the standard library.

73
00:04:57,290 --> 00:04:59,060
And I'm going to do that in.

74
00:05:00,190 --> 00:05:05,950
My idea, by hitting the car, holding down the Comanche and hitting or clicking on it, and that takes

75
00:05:05,950 --> 00:05:08,510
me right to this function f print f it returns.

76
00:05:08,530 --> 00:05:10,880
So here's the description of this function.

77
00:05:11,380 --> 00:05:14,500
The function is called F print F it's part of the package.

78
00:05:14,500 --> 00:05:16,990
If I scroll to the very top, I'm on line two hundred and two.

79
00:05:16,990 --> 00:05:18,910
I don't want to lose my line at the very top.

80
00:05:18,910 --> 00:05:22,770
It's part of the FMT package, so I'm going to go back to line two hundred and two.

81
00:05:23,830 --> 00:05:26,020
So it's a function as part of that package.

82
00:05:26,020 --> 00:05:33,670
It takes as arguments an IO writer and fortunately the response writer for that's part of the package

83
00:05:33,670 --> 00:05:35,070
satisfies this requirement.

84
00:05:35,080 --> 00:05:40,780
So it's a writer and then it has a format string and then optionally some other things after it, which

85
00:05:40,780 --> 00:05:41,890
I'm going to ignore for right now.

86
00:05:42,190 --> 00:05:43,600
So and it returns.

87
00:05:43,600 --> 00:05:44,660
This is the part I'm interested in.

88
00:05:44,660 --> 00:05:49,240
It returns an end, which is an entry and an error which is of type error.

89
00:05:49,540 --> 00:05:50,950
So let's read the description here.

90
00:05:51,280 --> 00:05:56,680
F print f format's according to a format specified and writes to W it returns the number of bytes written

91
00:05:56,830 --> 00:05:59,410
and any right error encountered.

92
00:05:59,440 --> 00:06:00,950
So I'm going to get the number of bytes written.

93
00:06:01,190 --> 00:06:07,930
I don't really want that, but I can look at it so I could say n that's a variable that's going to store

94
00:06:07,930 --> 00:06:13,510
the number of bytes written and the error, which is I'm going to call E R again.

95
00:06:13,510 --> 00:06:17,890
I can call it whatever I want to and then I think it might just go equals, but that's not going to

96
00:06:17,890 --> 00:06:18,260
work.

97
00:06:18,640 --> 00:06:22,120
Now that is actually a function we're going to use a lot this this equals sign.

98
00:06:22,120 --> 00:06:28,720
But right now we're going to use a special one that says automatically format these return types to

99
00:06:28,720 --> 00:06:30,940
whatever this function sends back.

100
00:06:30,940 --> 00:06:35,710
And we saw in the description that F print F returns and int and an error.

101
00:06:35,710 --> 00:06:39,610
So N will be an or an integer and error will be of type error.

102
00:06:40,240 --> 00:06:43,930
I still have red lines underneath these things indicating there's an error.

103
00:06:44,340 --> 00:06:52,780
Actually if I try to compile this, if I say in my terminal, go, run Maeng go, I'll get an error

104
00:06:54,220 --> 00:06:59,620
and the error is actually two errors and declared but not used an error declared but not used.

105
00:06:59,650 --> 00:07:05,890
So I actually am not allowed in go to declare variables unless I actually use them.

106
00:07:06,280 --> 00:07:09,210
I don't really care about the number of bytes written, but let's do something with it.

107
00:07:09,220 --> 00:07:15,610
OK, format f print line and then I'm going to put.

108
00:07:17,140 --> 00:07:27,170
Bytes written all by column and then a plus sign to join what I'm passing it next, and that would be

109
00:07:27,170 --> 00:07:28,290
when you think that would work.

110
00:07:28,290 --> 00:07:34,410
If you're coming from the AP world, that would make perfect sense, but it doesn't because AP is loosely

111
00:07:34,410 --> 00:07:40,500
typed, which means I can store anything I want in a variable, whereas GO is considerably more strict.

112
00:07:40,500 --> 00:07:45,320
It says M is an aunt and you can't print an event like that.

113
00:07:45,510 --> 00:07:49,440
Well, how am I going to print out the number of bytes to the terminal window, which is all I'm trying

114
00:07:49,440 --> 00:07:49,830
to do?

115
00:07:50,610 --> 00:07:51,570
Well, it's not that hard.

116
00:07:51,870 --> 00:07:55,650
Instead of format print line, I'm going to go format.

117
00:07:57,710 --> 00:08:01,580
Print line, I'm still going to use that, and I don't know your format again.

118
00:08:02,790 --> 00:08:09,920
S print F and s print F allows me to take different data types and return them as a string.

119
00:08:09,930 --> 00:08:16,480
So in here I'm going to put a number of bytes written colon and then I'm going to put a placeholder.

120
00:08:16,900 --> 00:08:23,850
D and D is a placeholder that F format s print F is going to replace with some value.

121
00:08:23,860 --> 00:08:27,460
And as you might guess, I want to put any of them there.

122
00:08:27,630 --> 00:08:28,590
OK, so that works.

123
00:08:28,590 --> 00:08:30,960
But I still have an error that I'm not doing anything with.

124
00:08:31,500 --> 00:08:33,170
All right, let's handle that.

125
00:08:34,370 --> 00:08:40,230
Since error is returned by the F print F function, and if there is no error, it doesn't have any value.

126
00:08:40,280 --> 00:08:45,950
I can actually do a simple if statement and an if statement is nothing more than checking one condition

127
00:08:45,950 --> 00:08:52,220
against another and then executing a particular set of commands if the conditions meet whatever criteria

128
00:08:52,220 --> 00:08:52,920
I specified.

129
00:08:52,940 --> 00:08:53,590
So let's try it.

130
00:08:53,600 --> 00:08:56,230
If error is not equal to nil.

131
00:08:56,420 --> 00:09:00,640
In other words, if there is an error, I don't know what it's going to be.

132
00:09:00,650 --> 00:09:04,430
But I know if there is no error, it's going to be nil that I want to do something.

133
00:09:04,430 --> 00:09:08,530
And all I'm going to do here is format, print, line error.

134
00:09:08,840 --> 00:09:14,090
I'm just going to write the error to the console, which is not terribly helpful in a real world program,

135
00:09:14,420 --> 00:09:16,850
but it's suitable for our requirements right now.

136
00:09:17,660 --> 00:09:20,270
So I've now solved the problem.

137
00:09:20,270 --> 00:09:25,730
I've got those two return values from F print F and I handle one.

138
00:09:25,730 --> 00:09:26,930
If there's an error, I print it.

139
00:09:26,930 --> 00:09:29,840
If there's not, nothing happens and then I just print the number of bytes written.

140
00:09:29,850 --> 00:09:30,800
So let's try that.

141
00:09:30,800 --> 00:09:31,720
Let's run this again.

142
00:09:31,730 --> 00:09:33,020
So back down to my terminal.

143
00:09:33,320 --> 00:09:39,020
I hit the up arrow to get my last command and I run this command and then it goes and says, OK, well,

144
00:09:39,620 --> 00:09:40,490
nothing happened.

145
00:09:40,490 --> 00:09:44,840
It started and it ended y well it's pretty straightforward.

146
00:09:44,840 --> 00:09:50,750
I have this HTP handler function which is listening for a request sent by a web browser.

147
00:09:51,140 --> 00:09:57,340
And yet at no point have I started any process that says listen for requests from Web browsers.

148
00:09:57,380 --> 00:10:02,450
So I actually have to do something else beyond this handler function, which in fact is never actually

149
00:10:02,450 --> 00:10:02,900
called.

150
00:10:03,200 --> 00:10:08,000
It's just sitting there embedded in my main function and it never executes.

151
00:10:08,570 --> 00:10:19,280
So now here's how easy it is to start a Web server that listens for requests in go http dot listen and

152
00:10:19,280 --> 00:10:19,940
serve.

153
00:10:20,510 --> 00:10:23,450
And I'm going to listen to a particular part of my computer.

154
00:10:23,690 --> 00:10:28,820
And because I don't want to use a privileged port and ports are just things that you can listen to from

155
00:10:28,820 --> 00:10:29,480
applications.

156
00:10:30,050 --> 00:10:35,150
The first one thousand and twenty four ports on any computer system are privileged.

157
00:10:35,150 --> 00:10:37,940
You have to be a super user to use those, which is no problem.

158
00:10:37,940 --> 00:10:40,160
I'm going to listen to Port and it has to start with the colon.

159
00:10:40,590 --> 00:10:41,170
Eighty, eighty.

160
00:10:42,050 --> 00:10:46,340
So I'm going to listen on port eighty eighty and then it expects another argument.

161
00:10:46,340 --> 00:10:46,940
A handler.

162
00:10:46,940 --> 00:10:48,500
But I'm going to say nil.

163
00:10:48,890 --> 00:10:49,940
I'm not going to pass it.

164
00:10:49,940 --> 00:10:51,650
A particular handler instead.

165
00:10:51,650 --> 00:10:54,350
That's what this HDB handle function does.

166
00:10:54,380 --> 00:10:56,000
I don't need this anymore so I'm going to delete it.

167
00:10:57,590 --> 00:11:01,430
And now when I run this notice, this also has an error.

168
00:11:01,430 --> 00:11:04,040
It's listening for and I'm going to ignore it explicitly.

169
00:11:04,040 --> 00:11:06,620
I'm going to do that because I don't want to handle this error.

170
00:11:06,620 --> 00:11:09,740
If the Web server doesn't start, I will know because the program will crash.

171
00:11:09,740 --> 00:11:11,060
So I don't need to look for this error.

172
00:11:11,450 --> 00:11:12,320
I'm going to say.

173
00:11:13,650 --> 00:11:20,970
Underline so the shift dash equals that and all this is saying is if there's an error, I don't care,

174
00:11:20,970 --> 00:11:23,840
just throw it in the trash bin, pay no attention to it whatsoever.

175
00:11:24,120 --> 00:11:26,480
So let's run this program again and see what happens.

176
00:11:26,700 --> 00:11:27,720
Go, run, go.

177
00:11:28,830 --> 00:11:32,990
And nothing seems to be happening, but it didn't kick me out of the application.

178
00:11:33,270 --> 00:11:40,410
What I have just done with one line of code is started a production quality web server to listen for

179
00:11:40,410 --> 00:11:41,100
requests.

180
00:11:41,340 --> 00:11:42,930
So let's make sure this actually works.

181
00:11:43,080 --> 00:11:45,780
What I expect to happen is I'm going to go to my Web browser.

182
00:11:46,110 --> 00:11:50,100
I'm going to go to this server, which is called localhost on any computer.

183
00:11:50,100 --> 00:11:53,130
That's the default localhost Colen eighty eighty.

184
00:11:53,460 --> 00:11:56,630
And what I want to see happen is two things I want to see.

185
00:11:56,640 --> 00:12:02,010
Hello, world printed to my Web browser window, and then I want to be able to come back in here and

186
00:12:02,010 --> 00:12:06,380
I should see a log entry right down here in the terminal that tells me how many bytes were written.

187
00:12:06,480 --> 00:12:07,410
Let's see if that works.

188
00:12:08,130 --> 00:12:14,280
So I go to my favorite Web browser and I go to the URL bar and I type in local host Colen eight zero

189
00:12:14,280 --> 00:12:15,990
eight zero and it return.

190
00:12:16,680 --> 00:12:17,910
And there is hello world.

191
00:12:17,910 --> 00:12:18,480
Perfect.

192
00:12:18,510 --> 00:12:20,340
Now let's go back and look at our terminal window.

193
00:12:21,420 --> 00:12:25,290
And there it is, number of bytes written, 13 number of bytes written 13.

194
00:12:25,320 --> 00:12:26,710
Now, why did that run it twice?

195
00:12:26,730 --> 00:12:27,450
I'm not sure.

196
00:12:27,490 --> 00:12:28,800
Don't really care about that right now.

197
00:12:29,040 --> 00:12:30,650
I'm going to let that do its thing.

198
00:12:31,380 --> 00:12:35,250
So now I have written a complete Web application.

199
00:12:35,290 --> 00:12:35,730
True.

200
00:12:35,730 --> 00:12:40,170
All it does actually serve the words hello, karma world exclamation mark.

201
00:12:40,560 --> 00:12:42,750
But it does it really, really well.

202
00:12:42,960 --> 00:12:50,010
And this is the basis for any web application and go now things will get more complex, but not that

203
00:12:50,010 --> 00:12:50,790
complex.

204
00:12:50,790 --> 00:12:57,480
One of the great things about Go is it's an extremely easy to learn language and everything functions

205
00:12:57,480 --> 00:13:02,670
in a really straightforward, organized and logical way, and you're not going to have any difficulty

206
00:13:02,670 --> 00:13:03,410
following along.

207
00:13:03,930 --> 00:13:05,190
So that's enough for this time.

208
00:13:05,400 --> 00:13:07,650
Next time will make it a little bit more complex.
