1
00:00:01,020 --> 00:00:06,900
So we're looking for a way to avoid loading the template, cash, the entire cash every single time

2
00:00:06,900 --> 00:00:12,360
we display a page on the site and what we're really talking about is setting configurations, because

3
00:00:12,360 --> 00:00:18,660
I want to set the configuration such that once I have this template set, I never want to load it again

4
00:00:18,660 --> 00:00:24,180
until the application restarts or I issue a command that says load the template cache again for whatever

5
00:00:24,180 --> 00:00:24,550
reason.

6
00:00:25,230 --> 00:00:31,440
So in order to do that, we effectively need to set up some kind of application wide configuration setting.

7
00:00:31,890 --> 00:00:35,100
Now, a very common method of doing this and other programming languages.

8
00:00:35,460 --> 00:00:36,660
Unfortunately, it's common.

9
00:00:36,660 --> 00:00:42,420
It shouldn't be is to use global variables to have variables that are available to every every part

10
00:00:42,420 --> 00:00:44,840
of the application at all times, no matter what.

11
00:00:45,360 --> 00:00:47,470
Well, we're going to use a slightly better approach.

12
00:00:47,490 --> 00:00:49,610
I'm going to do that by creating a new package.

13
00:00:49,890 --> 00:00:51,000
So I'm going to go to package.

14
00:00:51,360 --> 00:00:53,380
And the reason I'm I'll tell you why I'm creating it.

15
00:00:53,400 --> 00:00:54,900
This in its own package at the moment.

16
00:00:55,380 --> 00:01:03,080
I want to create a new folder and I'm going to call that config config.

17
00:01:03,570 --> 00:01:09,980
And inside of that, I'll create a new go file and I'll just call that config go.

18
00:01:10,650 --> 00:01:12,720
So it creates a new package.

19
00:01:13,140 --> 00:01:20,460
And the reason I'm going to create a package here is this configuration file might be accessed from

20
00:01:20,460 --> 00:01:21,690
any part of my application.

21
00:01:22,260 --> 00:01:30,360
And I'm going to be very careful to ensure that this configuration file doesn't import anything other

22
00:01:30,360 --> 00:01:32,130
than what it absolutely has to.

23
00:01:32,160 --> 00:01:38,100
What I'm trying to avoid is having applications by application set up such that packages in part from

24
00:01:38,100 --> 00:01:42,420
each other all over the place without regard to any kind of logic whatsoever.

25
00:01:42,750 --> 00:01:47,430
And the danger of doing that is you're going to run into something called an import cycle and your app

26
00:01:47,430 --> 00:01:48,930
will not compile.

27
00:01:49,230 --> 00:01:52,440
And that can be an extremely difficult error to avoid.

28
00:01:52,890 --> 00:01:59,400
So what I'm going to make sure here is that this package is imported where it needs to be by other parts

29
00:01:59,400 --> 00:02:00,240
of the application.

30
00:02:00,240 --> 00:02:05,850
So config is imported by other parts of the application, but it doesn't import anything else from the

31
00:02:05,850 --> 00:02:06,780
application itself.

32
00:02:06,780 --> 00:02:09,350
It only uses things that are built into the standard library.

33
00:02:09,570 --> 00:02:11,830
And this will help me avoid that kind of problem.

34
00:02:12,390 --> 00:02:13,500
So what am I going to do here?

35
00:02:13,500 --> 00:02:16,740
I am going to this is going to consist of a single struct.

36
00:02:16,740 --> 00:02:17,820
It's a type actually.

37
00:02:18,270 --> 00:02:23,790
It's a type called app config, which I'll name properly config.

38
00:02:24,090 --> 00:02:25,380
And it's just a struct.

39
00:02:25,380 --> 00:02:28,660
It's just a structure that holds certain kinds of information.

40
00:02:28,980 --> 00:02:32,460
And right now, all I want to put in there is my template cache.

41
00:02:32,670 --> 00:02:34,320
So I'll call it template cache.

42
00:02:34,320 --> 00:02:37,260
And I gave it a capital letter is the beginning part of the name.

43
00:02:37,260 --> 00:02:42,510
So that is visible and it's of type exactly the type that we have for our template cache.

44
00:02:42,510 --> 00:02:50,940
It's a map of string for the index and the content of each entry is a pointer to a template dot template.

45
00:02:52,080 --> 00:02:52,740
And there it is.

46
00:02:52,920 --> 00:02:57,000
So now I've created this now because this is something that should be commented.

47
00:02:57,000 --> 00:02:57,990
Let's give it a comment.

48
00:03:00,750 --> 00:03:04,860
Holds the application config.

49
00:03:05,620 --> 00:03:06,030
All right.

50
00:03:06,190 --> 00:03:07,200
A special application.

51
00:03:07,200 --> 00:03:07,470
Right.

52
00:03:09,050 --> 00:03:13,680
So I've created this and I'm not using anywhere right now, but I've created it.

53
00:03:13,680 --> 00:03:20,010
And because of the struct, I can put all kinds of things in here, anything I need site wide for my

54
00:03:20,010 --> 00:03:22,230
application, any kind of configuration setting.

55
00:03:22,350 --> 00:03:28,170
I can just add an entry to app config and it will be available to every package that imports this particular

56
00:03:28,170 --> 00:03:28,710
package.

57
00:03:29,580 --> 00:03:30,960
So I've created it.

58
00:03:31,110 --> 00:03:37,160
Now let's go back to may not go our very start of our application and let's get this going.

59
00:03:37,200 --> 00:03:38,970
So I'm going to create a variable here.

60
00:03:38,970 --> 00:03:44,880
Very first thing in my main function is to create a variable called app and it's going to be a type

61
00:03:44,880 --> 00:03:46,680
config app config.

62
00:03:47,490 --> 00:03:51,600
And you notice what I did that it imported it up here as it should, and it gives me the warning.

63
00:03:51,600 --> 00:03:56,020
You're not using this anywhere, so I'm not going to compile until you actually do something with it.

64
00:03:57,540 --> 00:04:02,980
So now that I've got that done, how do I actually get my template cache where it is?

65
00:04:03,000 --> 00:04:06,960
Well, right now, back into render, we're calling it right here, create template cache.

66
00:04:06,960 --> 00:04:09,540
And I don't want to do that here anymore, so I'll get rid of that in a minute.

67
00:04:09,540 --> 00:04:13,460
Instead, I want to call it here in main go.

68
00:04:13,890 --> 00:04:15,100
So can I do that?

69
00:04:15,120 --> 00:04:16,230
Well, let's find out.

70
00:04:16,620 --> 00:04:20,400
Let's say let's first of all see what that returns.

71
00:04:20,490 --> 00:04:26,900
So template cache right here returns a map of string, template, template and an error.

72
00:04:27,150 --> 00:04:31,260
OK, so let's let's try see if we can get one.

73
00:04:32,730 --> 00:04:39,330
I'm going to go over here and say my template cache and an error is equal to and that's stored in the

74
00:04:39,330 --> 00:04:44,670
package render rendered ago a render that create template cache.

75
00:04:45,480 --> 00:04:45,900
All right.

76
00:04:45,900 --> 00:04:48,900
So I've now created these variables.

77
00:04:48,930 --> 00:04:50,100
I should test for my error.

78
00:04:50,100 --> 00:04:55,350
If error is not equal to Neil, then logged fatal because I can't load it.

79
00:04:55,350 --> 00:04:59,760
So I want to dialogued up fadhel cannot.

80
00:05:00,420 --> 00:05:04,840
Create template cash, so that's fine.

81
00:05:06,000 --> 00:05:13,740
Now I've got the template cash, let's start in the app app template cash equals Terzieff and that all

82
00:05:13,740 --> 00:05:16,530
should compile, so let's make sure it actually runs.

83
00:05:16,580 --> 00:05:23,220
Make sure that everything's actually going to compile, go run command web.

84
00:05:23,570 --> 00:05:25,620
Mean Dutko or start go.

85
00:05:28,810 --> 00:05:33,220
Yet the comp. Let's go over to our Web browser and make sure things still work, we're not using that

86
00:05:33,220 --> 00:05:33,890
configuration.

87
00:05:33,910 --> 00:05:34,250
Yes.

88
00:05:34,270 --> 00:05:36,020
So it actually works the way that it should.

89
00:05:36,220 --> 00:05:36,930
No problem.

90
00:05:37,090 --> 00:05:37,530
Perfect.

91
00:05:38,080 --> 00:05:41,660
So I've created that template cache I started in my application.

92
00:05:41,950 --> 00:05:48,190
Now I want to use what's stored in this application, this this site wide config.

93
00:05:48,430 --> 00:05:54,100
I want to use that over in my render function instead of rendering the template the way that I am here

94
00:05:54,100 --> 00:05:59,140
by calling this template cache, I want to somehow let's put a comment in here to say what we're going

95
00:05:59,140 --> 00:05:59,560
to do.

96
00:06:01,710 --> 00:06:05,740
Yet the template cash from the app config.

97
00:06:06,570 --> 00:06:07,470
That's what I want to do.

98
00:06:07,560 --> 00:06:12,140
So this is all going to go away and then the rest of it will stay pretty much the same.

99
00:06:12,150 --> 00:06:15,490
I just don't want to create the template cache every single time I do this.

100
00:06:15,930 --> 00:06:19,950
So how do I get in this render template function?

101
00:06:20,130 --> 00:06:21,740
How do I get that app config?

102
00:06:21,750 --> 00:06:29,430
In other words, this thing I've created over here, this app which is of type config and currently

103
00:06:29,430 --> 00:06:36,090
only holds the template cache, how do I get it from here where I've created it over to here where I

104
00:06:36,090 --> 00:06:36,880
need to use it.

105
00:06:37,260 --> 00:06:40,200
Well we'll do that in the next lecture and it's a pretty straightforward process.
