So we're looking for a way to avoid loading the template, cash, the entire cash every single time
we display a page on the site and what we're really talking about is setting configurations, because
I want to set the configuration such that once I have this template set, I never want to load it again
until the application restarts or I issue a command that says load the template cache again for whatever
reason.
So in order to do that, we effectively need to set up some kind of application wide configuration setting.
Now, a very common method of doing this and other programming languages.
Unfortunately, it's common.
It shouldn't be is to use global variables to have variables that are available to every every part
of the application at all times, no matter what.
Well, we're going to use a slightly better approach.
I'm going to do that by creating a new package.
So I'm going to go to package.
And the reason I'm I'll tell you why I'm creating it.
This in its own package at the moment.
I want to create a new folder and I'm going to call that config config.
And inside of that, I'll create a new go file and I'll just call that config go.
So it creates a new package.
And the reason I'm going to create a package here is this configuration file might be accessed from
any part of my application.
And I'm going to be very careful to ensure that this configuration file doesn't import anything other
than what it absolutely has to.
What I'm trying to avoid is having applications by application set up such that packages in part from
each other all over the place without regard to any kind of logic whatsoever.
And the danger of doing that is you're going to run into something called an import cycle and your app
will not compile.
And that can be an extremely difficult error to avoid.
So what I'm going to make sure here is that this package is imported where it needs to be by other parts
of the application.
So config is imported by other parts of the application, but it doesn't import anything else from the
application itself.
It only uses things that are built into the standard library.
And this will help me avoid that kind of problem.
So what am I going to do here?
I am going to this is going to consist of a single struct.
It's a type actually.
It's a type called app config, which I'll name properly config.
And it's just a struct.
It's just a structure that holds certain kinds of information.
And right now, all I want to put in there is my template cache.
So I'll call it template cache.
And I gave it a capital letter is the beginning part of the name.
So that is visible and it's of type exactly the type that we have for our template cache.
It's a map of string for the index and the content of each entry is a pointer to a template dot template.
And there it is.
So now I've created this now because this is something that should be commented.
Let's give it a comment.
Holds the application config.
All right.
A special application.
Right.
So I've created this and I'm not using anywhere right now, but I've created it.
And because of the struct, I can put all kinds of things in here, anything I need site wide for my
application, any kind of configuration setting.
I can just add an entry to app config and it will be available to every package that imports this particular
package.
So I've created it.
Now let's go back to may not go our very start of our application and let's get this going.
So I'm going to create a variable here.
Very first thing in my main function is to create a variable called app and it's going to be a type
config app config.
And you notice what I did that it imported it up here as it should, and it gives me the warning.
You're not using this anywhere, so I'm not going to compile until you actually do something with it.
So now that I've got that done, how do I actually get my template cache where it is?
Well, right now, back into render, we're calling it right here, create template cache.
And I don't want to do that here anymore, so I'll get rid of that in a minute.
Instead, I want to call it here in main go.
So can I do that?
Well, let's find out.
Let's say let's first of all see what that returns.
So template cache right here returns a map of string, template, template and an error.
OK, so let's let's try see if we can get one.
I'm going to go over here and say my template cache and an error is equal to and that's stored in the
package render rendered ago a render that create template cache.
All right.
So I've now created these variables.
I should test for my error.
If error is not equal to Neil, then logged fatal because I can't load it.
So I want to dialogued up fadhel cannot.
Create template cash, so that's fine.
Now I've got the template cash, let's start in the app app template cash equals Terzieff and that all
should compile, so let's make sure it actually runs.
Make sure that everything's actually going to compile, go run command web.
Mean Dutko or start go.
Yet the comp. Let's go over to our Web browser and make sure things still work, we're not using that
configuration.
Yes.
So it actually works the way that it should.
No problem.
Perfect.
So I've created that template cache I started in my application.
Now I want to use what's stored in this application, this this site wide config.
I want to use that over in my render function instead of rendering the template the way that I am here
by calling this template cache, I want to somehow let's put a comment in here to say what we're going
to do.
Yet the template cash from the app config.
That's what I want to do.
So this is all going to go away and then the rest of it will stay pretty much the same.
I just don't want to create the template cache every single time I do this.
So how do I get in this render template function?
How do I get that app config?
In other words, this thing I've created over here, this app which is of type config and currently
only holds the template cache, how do I get it from here where I've created it over to here where I
need to use it.
Well we'll do that in the next lecture and it's a pretty straightforward process.