1
00:00:00,840 --> 00:00:02,960
OK, so we left off a boat here.

2
00:00:03,030 --> 00:00:04,430
Let me get over to our code.

3
00:00:05,160 --> 00:00:08,340
What I want to do this time around is three things.

4
00:00:08,460 --> 00:00:14,370
First of all, I want to enable go modules which will be using fairly extensively through the remainder

5
00:00:14,370 --> 00:00:15,180
of this course.

6
00:00:15,690 --> 00:00:21,960
Secondly, I want to rearrange things again to support go modules to make it easier to support go modules.

7
00:00:22,530 --> 00:00:28,120
And thirdly, I want to use templates from a layout and pass data to them.

8
00:00:28,140 --> 00:00:31,250
OK, so those are the things we're going to try to accomplish in this lecture.

9
00:00:31,290 --> 00:00:33,250
And fortunately, none of it is that difficult.

10
00:00:33,690 --> 00:00:37,980
So I'm going to go to my terminal and I am going to stop my application.

11
00:00:37,980 --> 00:00:43,290
If I have it running, I'm going to make sure I'm in the root level of my directory and I am minus called

12
00:00:43,290 --> 00:00:44,010
Hello World.

13
00:00:44,430 --> 00:00:49,940
And now I'm going to enable go modules and go modules is basically package management for go.

14
00:00:50,280 --> 00:00:59,190
So it's like using composer in for example, it allows us to easily use external third party packages

15
00:00:59,640 --> 00:01:04,860
that have pre-built functionality that we want to use so we don't have to reinvent the wheel.

16
00:01:05,430 --> 00:01:06,990
So doing that is pretty simple.

17
00:01:07,020 --> 00:01:14,130
I'm in the root level of my module and I'm going to say go mud in it and then I need to give it a name.

18
00:01:14,550 --> 00:01:21,480
And conventionally, the name of your package or your application should correspond to where it exists

19
00:01:21,750 --> 00:01:24,230
in a git repository repository somewhere.

20
00:01:24,240 --> 00:01:29,970
So I'm going to use GitHub, even though this repository might not exist, GitHub, dotcom, and then

21
00:01:29,970 --> 00:01:34,140
I'm going to use my first my name or whatever my GitHub username is T Sollar.

22
00:01:35,850 --> 00:01:38,520
And then I'm going to name my package, which I'll just call go course.

23
00:01:39,270 --> 00:01:40,170
And when I do that.

24
00:01:41,140 --> 00:01:47,350
It creates a new module, and if I look up in my directory where all my files are, it's created a new

25
00:01:47,350 --> 00:01:49,690
file called Go Dormant.

26
00:01:49,900 --> 00:01:56,190
And if I open that, it's just a text file and all it does is name this module, GitHub, Dotcom, TESOL,

27
00:01:56,200 --> 00:01:57,690
Argo, dash course.

28
00:01:58,240 --> 00:02:01,300
And it tells me I'm using go version one point one five.

29
00:02:01,480 --> 00:02:02,740
You might have a different version there.

30
00:02:02,740 --> 00:02:06,870
It might be later when you take this course, we might be able to go to who knows, but it doesn't matter.

31
00:02:06,910 --> 00:02:08,230
That's what that file is going to be.

32
00:02:08,590 --> 00:02:12,540
Now, this is a file that we very rarely will ever edit by hand.

33
00:02:12,640 --> 00:02:16,360
There are situations where you're going to, but not always.

34
00:02:16,690 --> 00:02:22,390
Now, you'll notice in my Ebeid, I got a little warning here in the lower right hand corner that says

35
00:02:22,390 --> 00:02:23,950
go module is detected.

36
00:02:24,100 --> 00:02:28,270
Module path is then examine the past of the module, enable integration.

37
00:02:28,280 --> 00:02:30,370
Yes, I'm going to enable the integration.

38
00:02:30,580 --> 00:02:33,280
So I'm going to click enable integration.

39
00:02:33,460 --> 00:02:37,270
Check this box that says enable it and click OK.

40
00:02:37,990 --> 00:02:38,650
And it's done.

41
00:02:38,710 --> 00:02:40,300
Gopalnath indexing is disabled.

42
00:02:40,300 --> 00:02:40,930
That's fine.

43
00:02:40,930 --> 00:02:41,800
I don't care about that.

44
00:02:41,800 --> 00:02:42,970
I'm using good modules.

45
00:02:43,330 --> 00:02:46,690
This is the way that module that go programming is done these days.

46
00:02:46,690 --> 00:02:52,210
Using the modules, you'll find all kinds of tutorials online that tell you the old way of doing it

47
00:02:52,210 --> 00:02:54,250
by setting your gopalnath variable and all that.

48
00:02:54,250 --> 00:02:56,740
But we're going to use modules because that's the way it is done now.

49
00:02:56,920 --> 00:02:57,310
All right.

50
00:02:57,310 --> 00:02:58,030
So that was easy.

51
00:02:58,060 --> 00:02:59,290
So I've created a module.

52
00:03:00,370 --> 00:03:01,900
Now I want to do something else.

53
00:03:01,900 --> 00:03:07,720
I want to refactor my code, rearrange where it exists so that it makes a little more sense.

54
00:03:07,720 --> 00:03:12,190
And I'm going to do that, first of all, by going to my hello world directory at the root level of

55
00:03:12,190 --> 00:03:14,140
my application and creating a new directory.

56
00:03:14,950 --> 00:03:18,340
And I'm going to call that CMD for command.

57
00:03:18,340 --> 00:03:23,230
Inside of that, I'm going to create another new directory called WEAP.

58
00:03:24,130 --> 00:03:25,480
Now, this is kind of conventional.

59
00:03:25,510 --> 00:03:30,240
This is typically where Web applications often have their main function.

60
00:03:30,760 --> 00:03:35,890
So what I'm going to do is move my main function over into the command web.

61
00:03:36,370 --> 00:03:38,070
And it asked me, do I want to move that?

62
00:03:38,080 --> 00:03:40,330
Yes, I do want to refactor that so it moves it.

63
00:03:40,960 --> 00:03:41,860
So that's great.

64
00:03:41,860 --> 00:03:46,150
But immediately I see I have an error and the error is one that's pretty straightforward to understand.

65
00:03:46,150 --> 00:03:47,410
I have mango.

66
00:03:47,410 --> 00:03:51,340
If I open that it says I can't find home and I can't find a boat.

67
00:03:51,640 --> 00:03:56,020
That's because these handlers are no longer part of the main package.

68
00:03:56,020 --> 00:04:01,840
They say that they're they say package mean, but packages always live in their own directory.

69
00:04:01,960 --> 00:04:05,740
So what I'm going to do is move handlers to its own package.

70
00:04:06,130 --> 00:04:10,810
So I'm going to go back to my top level of my application and create a new directory.

71
00:04:12,160 --> 00:04:15,400
And I'm going to call that pkg for packages.

72
00:04:15,400 --> 00:04:18,790
That's where I'm going to put all the packages that I build from my application.

73
00:04:18,790 --> 00:04:20,440
And we'll have a few by the time we're done here.

74
00:04:20,980 --> 00:04:25,870
Inside of that, I'll create another new folder called Handlers.

75
00:04:27,280 --> 00:04:31,300
Then I will move my handlers into the handlers directory.

76
00:04:32,110 --> 00:04:35,050
And this I'd asked me, do you want to move it?

77
00:04:35,410 --> 00:04:37,270
And I say, yes, I do want to refactor it.

78
00:04:37,270 --> 00:04:38,380
So it moves them in there.

79
00:04:38,380 --> 00:04:41,140
And I look inside package and I have yet more errors.

80
00:04:41,140 --> 00:04:42,970
This seems to be getting worse instead of better.

81
00:04:43,540 --> 00:04:48,520
Well, the error is, again, it can't find the render template package or function because it's not

82
00:04:48,520 --> 00:04:49,570
part of this package.

83
00:04:50,290 --> 00:04:52,120
But you notice what I moved it in at refactored.

84
00:04:52,120 --> 00:04:55,570
My application is Handler's file to say it's now using the package handlers.

85
00:04:55,570 --> 00:04:55,930
Wow.

86
00:04:55,990 --> 00:04:58,180
OK, how come I can't find my render?

87
00:04:58,180 --> 00:05:05,410
Well, let's create another package, a new package or new directorate called Prender.

88
00:05:06,400 --> 00:05:06,600
Great.

89
00:05:06,790 --> 00:05:08,620
So I've got a new directory called Render.

90
00:05:08,620 --> 00:05:14,380
Now I will move my render package into or render file into this render package.

91
00:05:14,710 --> 00:05:16,090
And yes, I want to refactor it.

92
00:05:16,090 --> 00:05:24,190
So when I reopen it, it says, hey, you've got this a file and now it's part of the render package.

93
00:05:24,190 --> 00:05:30,220
Remember, all the files for a package must exist in the same directory.

94
00:05:30,820 --> 00:05:31,720
So you think about it.

95
00:05:31,720 --> 00:05:34,930
My main package is now in a folder called WEAP.

96
00:05:35,080 --> 00:05:39,720
I can call that whatever I wanted to, but this is a Web application, so I call it Web and that is

97
00:05:39,730 --> 00:05:40,120
package.

98
00:05:40,120 --> 00:05:46,510
Main Handler's package has one file in it called package handlers and the render template.

99
00:05:46,540 --> 00:05:50,140
The the things that were into the templates they're in a package called Renders.

100
00:05:50,140 --> 00:05:52,330
When I open it, its package is render.

101
00:05:53,380 --> 00:06:00,790
Now, you may recall a while ago I said that when you have a function in a package and the function

102
00:06:00,790 --> 00:06:06,370
name begins with a lowercase letter, that is a private function, you can only call that function from

103
00:06:06,370 --> 00:06:07,270
within the package.

104
00:06:07,600 --> 00:06:12,880
Well, inside the render package, I have a function called render template, which I'm using inside

105
00:06:12,880 --> 00:06:16,390
templates inside my handlers to actually render templates.

106
00:06:16,390 --> 00:06:21,070
But this begins with a lowercase letter, so let's rename it with an uppercase letter.

107
00:06:21,070 --> 00:06:22,060
Now it's visible.

108
00:06:22,060 --> 00:06:23,270
I can see it now.

109
00:06:23,330 --> 00:06:30,790
I'm going to put a comment in there like you're supposed to renders templates using HTML template,

110
00:06:31,240 --> 00:06:31,990
which I better spell.

111
00:06:31,990 --> 00:06:32,230
Right.

112
00:06:33,410 --> 00:06:34,450
So now this is visible.

113
00:06:34,450 --> 00:06:40,210
It means I can see it from within my handlers or any other package in my application, but I'm.

114
00:06:40,780 --> 00:06:44,160
To it here, not using a package name and with a lower case.

115
00:06:44,440 --> 00:06:45,760
So let's just change that.

116
00:06:45,820 --> 00:06:53,410
Let's go to render template and notice my I.D. said, hey, you want to use the render template package

117
00:06:53,410 --> 00:06:56,170
from the render the render template functions from the render package.

118
00:06:56,180 --> 00:06:57,160
That's the one I want.

119
00:06:57,700 --> 00:07:00,460
So it automatically imparts it and notice what it imported.

120
00:07:00,460 --> 00:07:07,480
It didn't didn't just say render it said GitHub, Dotcom, TESOL or Godus of course package render.

121
00:07:07,720 --> 00:07:13,660
So it's the full path name according to go module's to that package and now I can use it.

122
00:07:13,750 --> 00:07:18,730
I need to do the same thing for this function rendered template.

123
00:07:19,720 --> 00:07:20,410
I'm here now.

124
00:07:20,410 --> 00:07:23,760
I have no errors in this one, no errors in this one, no errors in this one.

125
00:07:23,770 --> 00:07:25,090
But I still have errors in May.

126
00:07:25,180 --> 00:07:25,750
Don't go.

127
00:07:25,780 --> 00:07:26,230
Why?

128
00:07:26,590 --> 00:07:30,700
Because again, I'm calling the home package and I actually can't.

129
00:07:31,000 --> 00:07:33,880
The home function, which doesn't exist anywhere in the main package.

130
00:07:34,450 --> 00:07:40,480
So instead I need to go to handlers and which this one's for home.

131
00:07:40,750 --> 00:07:42,520
Home there.

132
00:07:42,610 --> 00:07:45,880
And if I get rid of these parentheses, then life is good.

133
00:07:46,090 --> 00:07:49,990
I'm calling that actual handler, which now exists in his own package.

134
00:07:50,200 --> 00:07:51,460
And I can do the same thing here.

135
00:07:52,900 --> 00:07:59,530
Handlers dot about, OK, so that is actually all right now.

136
00:07:59,530 --> 00:08:02,320
So I have everything organized into their own packages.

137
00:08:02,500 --> 00:08:07,300
I have handlers that are just it's just going to consist of handlers for whatever pages our application

138
00:08:07,300 --> 00:08:08,440
might have, might have.

139
00:08:08,800 --> 00:08:14,050
I have the render that does nothing more than render templates and I have the main function which just

140
00:08:14,050 --> 00:08:18,910
calls those handlers and listens to the starts up a Web server that listens for connections.

141
00:08:19,180 --> 00:08:24,490
Let's try running this now because I mean the root level of my directory, which you can see again,

142
00:08:24,490 --> 00:08:27,190
I'm right here, users TC's Visual Studio Projects.

143
00:08:27,190 --> 00:08:27,850
Hello World.

144
00:08:28,660 --> 00:08:30,310
I can't go, go, run.

145
00:08:30,310 --> 00:08:31,150
StarTalk, go.

146
00:08:31,150 --> 00:08:33,220
Because there's no go files at this level.

147
00:08:33,400 --> 00:08:38,740
I actually need to change this to say go run command Web site Targo.

148
00:08:38,770 --> 00:08:42,160
And you might be asking yourself, what about all those files in the packages?

149
00:08:42,160 --> 00:08:43,400
How does it know where they are?

150
00:08:43,450 --> 00:08:45,370
Well, it knows because of the imports.

151
00:08:45,730 --> 00:08:50,350
So when I actually compile those files, only the only ones I'm worried about are the ones in this Web

152
00:08:50,350 --> 00:08:50,860
directory.

153
00:08:50,860 --> 00:08:52,360
And there's only one file there right now.

154
00:08:53,230 --> 00:08:58,840
It's actually going to compile those files and all the files that they depend upon from whatever packages

155
00:08:58,990 --> 00:09:01,020
that the application happens to use.

156
00:09:01,030 --> 00:09:05,530
So when I run this, everything should work and it runs.

157
00:09:05,800 --> 00:09:09,610
And if I go to my Web browser and load the about page, no problem.

158
00:09:10,060 --> 00:09:11,290
Load the home page.

159
00:09:11,290 --> 00:09:12,100
No problem.

160
00:09:12,340 --> 00:09:13,720
That worked really well.

161
00:09:14,110 --> 00:09:16,450
OK, so there's two more things I want to do.

162
00:09:16,990 --> 00:09:21,460
I want to pass data templates and I want to use layouts, but I'm going to put those into their own

163
00:09:21,460 --> 00:09:21,970
lectures.

164
00:09:21,970 --> 00:09:24,910
Just so if you need to come back, it's easier to find things.

165
00:09:24,910 --> 00:09:31,360
OK, so we'll move on in the very next lecture to using layouts and then in that lecture after that,

166
00:09:31,360 --> 00:09:35,260
we'll pass data to our HTML templates to our go templates.

167
00:09:35,680 --> 00:09:36,040
All right.

168
00:09:36,040 --> 00:09:37,030
See you in the next lecture.
