So in this section, I want to talk about routing and I want to use our first external package, so
if you look at our main DOT file, you'll notice that our routes are all set up here and that's OK.
But it's really kind of awkward when you start getting a more complex Web application to have all of
your routes right in your mean folder or your main routine.
So what I want to do is create a separate file.
So I'm going to go back to command web and create a new go file, which I will call routes that go all
right.
That's still part of our main package.
And in here, I'm going to have a function and I'm going to call it think routes.
And it's going to take one argument, which I will call map.
And it is, of course, our pointer to config up config and it's going to return and HTP Tandler and
you'll see why before too long, handler.
All right.
And inside of that, I'm actually going to create a new HTP handler, which we often call a mux or a
multiplexed.
And I'm going to do that using a package.
I'm not going to use the built in routing functions that are part of the gold standard library.
I want to use a third party package and we're going to use two, but we're going to start with one and
then we'll switch to the other.
We're going to use one that is actually really good at routing.
It's a very simple one.
And I'm going to go to my Web browser and show you what it is.
It's here at GitHub, dotcom BME area and why I have no idea how to pronounce that Pat.
And it's the router.
So this is a package that we can import and use in our own code.
So let's just scroll down and have a look at how do we install it.
Go get GitHub dot com slash.
All right, I'll copy that.
I will go back to my I.D., open the terminal window and I'll clear the screen and I'll just paste that
command right in there.
Go get GitHub, dot com, slash that B word, slash pat and it gets it.
All right.
Now, what happened when I did that?
Well, if I go and look at my go mod, you'll see that it now includes this package that was automatically
put in there for us.
All right.
So I've got it installed.
How do I use the darn thing?
Well, it's pretty straightforward.
First thing I'm going to do is create a multiplexed which which is actually at HTP Handler.
So I will say Mux is assigned the value of Pat Daudt knew.
All right.
So I now have a mux and I can just return that.
And in between where I create it and where I return it is where I actually set up my roots.
And we only have two routes right now, so let's put them in.
The first thing you should know is that Mux has some methods built in and you can see them listed here
in my Idy and you probably have a similar thing in yours.
And we want a get request.
And the request takes two arguments, a pattern, which is the string, which is simply the route we're
matching.
All right.
We can do that.
And then it takes an HTTP handler func.
Now we need to cast what we get to an active handler function.
And the function we want is the one that's built into our handlers and that's the home function.
So we can simply go handlers loops and
dot repo home and that.
There you go.
So that will actually wrote to our home function and we can do the same thing here about should go to
the about function.
All right.
So I've got this this function route's which takes app, which I'm not using, but I might need it at
some point, so I'm going to pass that.
How do we actually use this?
Well, again, that's not too complex.
All we need to do is make some changes in our main goal.
We're not going to use these anymore.
So Akhmatova's OK for right now.
And instead of this, which is listen and serve, I'm going to comment at that, too, as well.
So we're going to change it.
What we need to do is actually create a new variable called serve or whatever you want to call it.
I'm going to call it Serve S.V. because I am serving and I am going to make that equal to a pointer
to HDB server and that has some fields.
All right.
Those fields are what's our address apart?
Are we listening to.
Well, we know what that is.
That's our part number.
And we also want to pass it, our handlers.
And in this case, rather than writing them, as we did up here as individual functions, I'm actually
going to call our routes and pass at the app.
Got to Commissaire and Handler is singular, not plural, and this can't be that it needs to be that
a reference to the app and what do we want to do this?
That's all we really need to do at this point, is pass those in there.
We're going to improve this as time goes on.
But now I need to actually start that bit to start the actual server and we do it as follows.
Error equals serve, listen and serve.
And if there's an error, well, log that fatal error.
So when I run this now, if this compiles, let's see if it does, it is go run, command web, start,
go.
And I do need the sternau because now I have two files in my web application.
Let's hope this runs and it does.
So let's go back to our web browser and go to localhost eighty eighty and there it is and about and
there it is.
Perfect.
So now we've isolated our application routes so we can get rid of these.
Commented out outbids.
And have a look at our full main function and see what it looks like now, that's much cleaner.
So we're now getting down to the point where we can actually put our roots in one folder.
So the Pat Schroeder is fine for our purposes today.
But in the next lecture, I want to swap out Pat for another one called CZI, or possibly she I've never
actually heard it said out loud, but it's a different river and we're just going to do the swap to
show you how easy it can be with gold modules to switch it out.
Plus the key router has some built in middleware that will be using as well.
So we'll talk about that in the next lecture.