OK, so we left off a boat here.
Let me get over to our code.
What I want to do this time around is three things.
First of all, I want to enable go modules which will be using fairly extensively through the remainder
of this course.
Secondly, I want to rearrange things again to support go modules to make it easier to support go modules.
And thirdly, I want to use templates from a layout and pass data to them.
OK, so those are the things we're going to try to accomplish in this lecture.
And fortunately, none of it is that difficult.
So I'm going to go to my terminal and I am going to stop my application.
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
Hello World.
And now I'm going to enable go modules and go modules is basically package management for go.
So it's like using composer in for example, it allows us to easily use external third party packages
that have pre-built functionality that we want to use so we don't have to reinvent the wheel.
So doing that is pretty simple.
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.
And conventionally, the name of your package or your application should correspond to where it exists
in a git repository repository somewhere.
So I'm going to use GitHub, even though this repository might not exist, GitHub, dotcom, and then
I'm going to use my first my name or whatever my GitHub username is T Sollar.
And then I'm going to name my package, which I'll just call go course.
And when I do that.
It creates a new module, and if I look up in my directory where all my files are, it's created a new
file called Go Dormant.
And if I open that, it's just a text file and all it does is name this module, GitHub, Dotcom, TESOL,
Argo, dash course.
And it tells me I'm using go version one point one five.
You might have a different version there.
It might be later when you take this course, we might be able to go to who knows, but it doesn't matter.
That's what that file is going to be.
Now, this is a file that we very rarely will ever edit by hand.
There are situations where you're going to, but not always.
Now, you'll notice in my Ebeid, I got a little warning here in the lower right hand corner that says
go module is detected.
Module path is then examine the past of the module, enable integration.
Yes, I'm going to enable the integration.
So I'm going to click enable integration.
Check this box that says enable it and click OK.
And it's done.
Gopalnath indexing is disabled.
That's fine.
I don't care about that.
I'm using good modules.
This is the way that module that go programming is done these days.
Using the modules, you'll find all kinds of tutorials online that tell you the old way of doing it
by setting your gopalnath variable and all that.
But we're going to use modules because that's the way it is done now.
All right.
So that was easy.
So I've created a module.
Now I want to do something else.
I want to refactor my code, rearrange where it exists so that it makes a little more sense.
And I'm going to do that, first of all, by going to my hello world directory at the root level of
my application and creating a new directory.
And I'm going to call that CMD for command.
Inside of that, I'm going to create another new directory called WEAP.
Now, this is kind of conventional.
This is typically where Web applications often have their main function.
So what I'm going to do is move my main function over into the command web.
And it asked me, do I want to move that?
Yes, I do want to refactor that so it moves it.
So that's great.
But immediately I see I have an error and the error is one that's pretty straightforward to understand.
I have mango.
If I open that it says I can't find home and I can't find a boat.
That's because these handlers are no longer part of the main package.
They say that they're they say package mean, but packages always live in their own directory.
So what I'm going to do is move handlers to its own package.
So I'm going to go back to my top level of my application and create a new directory.
And I'm going to call that pkg for packages.
That's where I'm going to put all the packages that I build from my application.
And we'll have a few by the time we're done here.
Inside of that, I'll create another new folder called Handlers.
Then I will move my handlers into the handlers directory.
And this I'd asked me, do you want to move it?
And I say, yes, I do want to refactor it.
So it moves them in there.
And I look inside package and I have yet more errors.
This seems to be getting worse instead of better.
Well, the error is, again, it can't find the render template package or function because it's not
part of this package.
But you notice what I moved it in at refactored.
My application is Handler's file to say it's now using the package handlers.
Wow.
OK, how come I can't find my render?
Well, let's create another package, a new package or new directorate called Prender.
Great.
So I've got a new directory called Render.
Now I will move my render package into or render file into this render package.
And yes, I want to refactor it.
So when I reopen it, it says, hey, you've got this a file and now it's part of the render package.
Remember, all the files for a package must exist in the same directory.
So you think about it.
My main package is now in a folder called WEAP.
I can call that whatever I wanted to, but this is a Web application, so I call it Web and that is
package.
Main Handler's package has one file in it called package handlers and the render template.
The the things that were into the templates they're in a package called Renders.
When I open it, its package is render.
Now, you may recall a while ago I said that when you have a function in a package and the function
name begins with a lowercase letter, that is a private function, you can only call that function from
within the package.
Well, inside the render package, I have a function called render template, which I'm using inside
templates inside my handlers to actually render templates.
But this begins with a lowercase letter, so let's rename it with an uppercase letter.
Now it's visible.
I can see it now.
I'm going to put a comment in there like you're supposed to renders templates using HTML template,
which I better spell.
Right.
So now this is visible.
It means I can see it from within my handlers or any other package in my application, but I'm.
To it here, not using a package name and with a lower case.
So let's just change that.
Let's go to render template and notice my I.D. said, hey, you want to use the render template package
from the render the render template functions from the render package.
That's the one I want.
So it automatically imparts it and notice what it imported.
It didn't didn't just say render it said GitHub, Dotcom, TESOL or Godus of course package render.
So it's the full path name according to go module's to that package and now I can use it.
I need to do the same thing for this function rendered template.
I'm here now.
I have no errors in this one, no errors in this one, no errors in this one.
But I still have errors in May.
Don't go.
Why?
Because again, I'm calling the home package and I actually can't.
The home function, which doesn't exist anywhere in the main package.
So instead I need to go to handlers and which this one's for home.
Home there.
And if I get rid of these parentheses, then life is good.
I'm calling that actual handler, which now exists in his own package.
And I can do the same thing here.
Handlers dot about, OK, so that is actually all right now.
So I have everything organized into their own packages.
I have handlers that are just it's just going to consist of handlers for whatever pages our application
might have, might have.
I have the render that does nothing more than render templates and I have the main function which just
calls those handlers and listens to the starts up a Web server that listens for connections.
Let's try running this now because I mean the root level of my directory, which you can see again,
I'm right here, users TC's Visual Studio Projects.
Hello World.
I can't go, go, run.
StarTalk, go.
Because there's no go files at this level.
I actually need to change this to say go run command Web site Targo.
And you might be asking yourself, what about all those files in the packages?
How does it know where they are?
Well, it knows because of the imports.
So when I actually compile those files, only the only ones I'm worried about are the ones in this Web
directory.
And there's only one file there right now.
It's actually going to compile those files and all the files that they depend upon from whatever packages
that the application happens to use.
So when I run this, everything should work and it runs.
And if I go to my Web browser and load the about page, no problem.
Load the home page.
No problem.
That worked really well.
OK, so there's two more things I want to do.
I want to pass data templates and I want to use layouts, but I'm going to put those into their own
lectures.
Just so if you need to come back, it's easier to find things.
OK, so we'll move on in the very next lecture to using layouts and then in that lecture after that,
we'll pass data to our HTML templates to our go templates.
All right.
See you in the next lecture.