14 How to Understanding HTML Template Cache in Golang
Introduction to Cache Templates
In previous program codes that we have studied practically, they were not efficient. Why did it happen? because every time it is accessed it will call a function and describe the data sent into the template so the process requires high levels of execution. Every time the handler is called we will always re-parse the template. So ideally the template only performs parsing once at the beginning when the application is running, then only the template data is cached (stored in memory) so there is no need to access the data and do the parsing again so that the website we create can be faster.
13 How to Understanding HTML Template Function in Golang
Introduction to Function Templates
Apart from accessing fields
in templates, we can also access functions or functions
in Golang. The way to access a function is the same as accessing a field
, but if the function has parameters then we can use additional parameters that are sent when calling the function in the template.
12 How to Understanding HTML Template Layout in Golang
Introduction to Layout Templates
When we create a website page, there are several parts that are always the same on each page, for example header
and footer
, so if there are parts that are the same it is recommended that they be saved in a separate template so that they can be used in other templates or sometimes we call them reusable
. Now, this Golang template supports importing from other templates so we can use it to make website creation easier.
11 How to Understanding HTML Template Action in Golang
Introduction to Action Templates
Not only can we render text in templates, but we can also support action commands such as if
branching, for
loops and so on.
For example, suppose we use if
branching like this
10 How to Understanding HTML Template Data in Golang
Introduction to Data Templates
If you have studied HTML Templates in the previous article, then we will continue with data templates where we can display this data dynamically by using struct or map data. However, we need to know the changes in the template text. We need to know the name of the field or key that we will use to fill in the dynamic data in the template. We can mention the name of the field
or key
, for example {{.FieldName}}
.
09 How to Understanding HTML Template in Golang
Dynamic Web using Templates
In the previous post we discussed dynamic websites but using response strings contained in static files. So on this occasion we will try to get to know templating in Golang where the page, say HTML, will be dynamic and can change with the data accessed by the user.
08 How to Understanding File Server in Golang
Introduction to File Server
Golang has a feature called FileServer. With this we can create a Handler in Golang Web that we have used as a static file server and this FileServer does not need to be manually loaded again. So we can add it to http.Server
or http.ServeMux
.
07 How to Understanding Cookie in Golang
Introduction to Cookies
Before discussing Cookies, we need to know that HTTP is stateless between client and server, which means the server does not store any data to remember every request from the client. This aims to make it easy to scale the server itself. So how do you get the server to remember a client? for example, when we have logged in to a website, the server must automatically know that the client has logged in so that subsequent requests no longer require logging in. For things like this, we can usually use Cookies
.
06 How to Used Response Code in Golang
Introduction to Response Codes
Something we also need to know about HTTP is the response code. This is a representation of the response code, where from this code we can see whether a request we sent from the client was successfully processed by the server or failed to be processed by the server. So there are lots of response codes that we can use when creating a website. You can immediately look in more depth at several HTTP Status Codes here https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
05 How to Used Request Form Post in Golang
Introduction to Post Forms
Similar to the previous post, when we use the GET method, the results of all the data in the form will be a param query, whereas if we use POST then all the data in the form is sent via the body of the HTTP Request, only the method is different. All form post data sent from the client will automatically be stored in the Request.PostFrom
attribute. However, before we can retrieve the PostForm attribute data, we must first call the Request.ParseForm()
method and then this method will be used to parse the body. Parsing this data will produce form data, if it is not there it will cause an error in the parsing.
04 How to Used Request Header in Golang
Header Introduction
Apart from query parameters on HTTP, we can also use Headers. Headers are additional information that is usually sent from the client to the server or vice versa. In the Header, not only in the HTTP Request but in the HTTP Response we can also add header information. When we use a browser on our computer, usually headers will automatically be displayed by the browser such as browser information, types of content sent and received by the browser and much more.
03 How To Used Query Parameter in Golang
Introduction to Query Parameters
Query parameters are one of the features of http that we usually use to send data from the client to the server. This parameter query is placed in the URL of the endpoint that we have created. To add query parameters, we can use ?=name=value
in our website URL.