The first thing to handle is answering the question, "what are Go templates?" Go templates are a way to create dynamic content in Go by allowing the user to mix data with plain text or HTML files with data received from the Go code. This makes it possible to change the data in a template very easily, as it can be edited with ease. They are mainly used in web dev to create web pages that rely on user inputs or content from a database.
A template is just a file that has placeholders for data. In Go, placeholders are written using double curly braces {{ }}. These curly braces can hold a user’s data, and this can be shown by {{ .name }}. The dot refers to the data object you refer to from Go. When the template is running, Go replaces the {{ .name }} with the actual user name from the user's input or the database.
To use templates in Go, a data structure that can help you hold the data to show, usually a map or a struct, is required.
type PageData struct {
Title string
Message string
}
Then you parse the template file in your Go code and execute it with your data. The template system will automatically replace all placeholders with the correct values. This keeps your HTML or text separate from your Go code, making your program cleaner and easier to maintain.
A good example of template file is as shown below
<!DOCTYPE html>
<html>
<head>
<title>{{ .Title }}</title>
</head>
<body>
<h1>{{ .Title }}</h1>
<p>{{ .Message }}</p>
{{ if .IsLogged }}
<p>Welcome back, {{ .User }}! </p>
{{ else }}
<p>Please login to continue. </p>
{{ end }}
<h2>Your Tasks:</h2>
<ul>
{{ range .Tasks }}
<li>{{ . }}</li>
{{ else }}
<li>No tasks available.</li>
{{ end }}
</ul>
</body>
</html>
Go templates also support basic logic. This helps to manipulate the values to be displayed. You can display certain things if a certain condition is met. You can also range over maps, structs, among others. For example, you can display a specific message to only logged-in users, you can list all the content of a check list from a shopping list among many others. You can also use functions like len to make the content more flexible.
Below is an example of Go code that displays its output to the HTML file above.
package main
import (
"html/template"
"net/http"
)
type PageData struct {
Title string
Message string
IsLogged bool
User string
Tasks []string
}
func handler(w http.ResponseWriter, r *http.Request) {
data := PageData{
Title: "Go Templates Advanced Example",
Message: "Using conditions and loops in templates",
IsLogged: true,
User: "Ian",
Tasks: []string{"Learn Go templates", "Build a web app", "Practice Go daily"},
}
tmpl := template.Must(template.ParseFiles("index.html"))
tmpl.Execute(w, data)
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
In summary, to generate a dynamic text or HTML in Go, the best option is the Go template. They help separate the design of your output from your code logic, allow you to reuse templates with different data, and make it easy to build web pages, reports, or any text-based content.
Top comments (0)