Go Templates 01 - Basics

Go Templates 01 - Basics

Templates

Go provides a powerful template system through the text/template and html/template packages, enabling the creation of dynamic text and HTML outputs. While text/template is used for plain text generation, html/template is specifically designed for web applications, offering additional security against injection attacks.

Templates in Go use a declarative approach where placeholders, defined using {{ }}, are replaced with data at runtime.

This feature is widely utilized for generating emails, reports, configuration files, and dynamic web pages.

Understanding the concept of templates

This is one of the simplest examples of templates we can create, but it contains very important information about how data is sent to the template.

func main() {
    var tmplFile = "{{ . }}"
    tmpl, err := template.New("myFile").Parse(tmplFile)
    if err != nil {
        panic(err)
    }

    err = tmpl.Execute(os.Stdout, "Goku")
    if err != nil {
        panic(err)
    }
}        

More complex templates can be stored inside text files, but in our example this is our template:

var tmplFile = "{{ . }}"        

Everything inside the {{ }} tags will be interpreted at runtime.

tmpl, err := template.New("myFile").Parse(tmplFile)
if err != nil {
    panic(err)
}        

Here the tmpl variable was defined as a new template, myFile is just the name of the template, and in this example it will not make any difference which name is used.

Next we ask that a parse be performed on the contents of the tmplFile variable.

err = tmpl.Execute(os.Stdout, "Goku")
if err != nil {
    panic(err)
}        

Here we ask the template to be executed, we define its output which can be any destination that implements the io.Writer interface, and the data which in this case is just the string Goku

The value passed inside Execute is accessed by the {{ . }} marker

This way the output of our template execution is > Goku

Using structs with templates

In the same way that we pass a string for the template to be executed, we can pass a struct and access the fields of this struct within the template.

type User struct {
    Name     string
    LastName string
    Country  string
}

func main() {
    var tmplFile = `{{ .Name }} {{ .LastName }} {{ .Country }}`

    user := User{"John", "Doe", "USA"}

    tmpl, err := template.New("myFile").Parse(tmplFile)
    if err != nil {
        panic(err)
    }

    err = tmpl.Execute(os.Stdout, user)
    if err != nil {
        panic(err)
    }
}        

In the example above we are using the values of the ***struct*** inside the template with the markers {{ .Name }} {{ .LastName }} {{ .Country }}

Thus, the output of the above program will be: > John Doe USA

Using slices

In a more realistic scenario, we will need to work with slices, this way we can pass several values to be inserted into the template.

For this example we will create a file where the template will be, the name of the file will be template.tmpl

This is the content of the file template.tmpl

{{ range . }}
----------
Name: {{ .Name }} LastName: {{ .LastName }} Country: {{ .Country }}
{{ end }}        

The difference from the previous example is that we will now pass a slice to the function that executes the template.

type User struct {
    Name     string
    LastName string
    Country  string
}

func main() {
    tmplFile := "template.tmpl"

    users := []User{
        {"John", "Doe", "USA"},
        {"Gavin", "Steele", "USA"},
        {"Ashton", "Walsh", "CA"},
    }

    tmpl, err := template.New(tmplFile).ParseFiles(tmplFile)
    if err != nil {
        panic(err)
    }

    err = tmpl.Execute(os.Stdout, users)
    if err != nil {
        panic(err)
    }
}        

The range function inside the markers will go through the entire slice, and we will be able to access the fields of the struct in the same way as we did in the previous example.

Note that the {{ range . }} marker needs to be closed with the {{ end }} marker

This will be the output of our program:

----------
Name: John LastName: Doe Country: USA

----------
Name: Gavin LastName: Steele Country: USA

----------
Name: Ashton LastName: Walsh Country: CA        

Note that there are some line breaks in the text, but we can avoid this by adding the minus sign - to the {{ range -}} marker. With the minus sign:

{{ range . -}}
----------
Name: {{ .Name }} LastName: {{ .LastName }} Country: {{ .Country }}
{{ end }}        

This will be the result of our program:

----------
Name: John LastName: Doe Country: USA
----------
Name: Gavin LastName: Steele Country: USA
----------
Name: Ashton LastName: Walsh Country: CA        





要查看或添加评论,请登录

Daniel M.的更多文章

  • Golang HTTP Requests - Complete Guide

    Golang HTTP Requests - Complete Guide

    HTTP Requests In Go, we can use the net/http package to make http requests. We will use this public endpoint to make a…

  • Using structs with Redis and Golang

    Using structs with Redis and Golang

    Many times, we will need to store a struct in Redis instead of storing only separate fields. We can store a struct like…

  • Redis Hash

    Redis Hash

    A Redis Hash is a data structure in Redis that stores a set of key-value pairs, similar to an object or map in…

  • Go Nested Templates

    Go Nested Templates

    We can nest templates in Go, allowing us to create smaller parts of a template, similar to components. In this example,…

  • Go Templates 02 - Conditionals

    Go Templates 02 - Conditionals

    Conditional Templates Another very common feature we need when working with templates is the use of conditions. For all…

  • Go - sorting slices

    Go - sorting slices

    In Go, sorting a slice is often necessary when working with data that needs to be organized or structured for better…

  • Go - Arrays

    Go - Arrays

    Arrays Array is a data structure used to store a sequence of elements of same type in a fixed-size. An array is stored…

  • Redis SET command.

    Redis SET command.

    Redis set command SET We can set a string value to a key using the set command. In order to check if the value was…

  • Golang - Recursion

    Golang - Recursion

    Recursion The programming language Golang is known for its simplicity, efficiency and for work really well with…

社区洞察

其他会员也浏览了