Sure! Let's break down the concept of actions in Go templates, represented by `{{ ... }}`, with examples for each type: variables, function calls, loops, conditionals, and more. ### 1. Variables In Go templates, variables are placeholders that get replaced with actual data when the template is rendered. The data is typically passed to the template from the Go code. #### Example: ```go {{ .Title }} ``` Here, `{{ .Title }}` will be replaced by the value of the `Title` field in the data passed to the template. #### Go Code: ```go data := struct { Title string }{ Title: "Welcome to My Website", } tmpl.Execute(w, data) ``` #### Template: ```html {{ .Title }}

{{ .Title }}

``` ### 2. Function Calls Go templates allow you to call functions to process data before rendering it. Built-in functions can be used directly in the template. #### Example: ```go {{ .Date | dateFormat "2006-01-02" }} ``` Here, `{{ .Date | dateFormat "2006-01-02" }}` formats the date using the `dateFormat` function. #### Go Code: ```go data := struct { Date time.Time }{ Date: time.Now(), } tmpl.Execute(w, data) ``` #### Template: ```html Date Example

{{ .Date | dateFormat "2006-01-02" }}

``` ### 3. Loops Loops allow you to iterate over collections like arrays or slices. #### Example: ```go {{ range .Items }}
  • {{ . }}
  • {{ end }} ``` Here, `{{ range .Items }}` loops through each item in the `Items` collection. #### Go Code: ```go data := struct { Items []string }{ Items: []string{"Item 1", "Item 2", "Item 3"}, } tmpl.Execute(w, data) ``` #### Template: ```html Loop Example ``` ### 4. Conditionals Conditionals are used to include or exclude parts of the template based on certain conditions. #### Example: ```go {{ if .IsAdmin }}

    Welcome, admin!

    {{ else }}

    Welcome, user!

    {{ end }} ``` Here, `{{ if .IsAdmin }}` checks if the `IsAdmin` variable is true. #### Go Code: ```go data := struct { IsAdmin bool }{ IsAdmin: true, } tmpl.Execute(w, data) ``` #### Template: ```html Conditional Example {{ if .IsAdmin }}

    Welcome, admin!

    {{ else }}

    Welcome, user!

    {{ end }} ``` ### 5. Pipelines Pipelines allow you to chain multiple operations together. Each operation's output becomes the input for the next operation. #### Example: ```go {{ .Price | multiply 1.1 | printf "%.2f" }} ``` Here, `{{ .Price | multiply 1.1 | printf "%.2f" }}` multiplies `Price` by `1.1` and then formats it to two decimal places. #### Go Code: ```go data := struct { Price float64 }{ Price: 100.0, } tmpl.Execute(w, data) ``` #### Template: ```html Pipeline Example

    Price: ${{ .Price | multiply 1.1 | printf "%.2f" }}

    ``` ### 6. Custom Functions You can define custom functions in Go and pass them to the template. #### Example: Define a custom function to multiply a number: ```go func multiply(a, b float64) float64 { return a * b } ``` #### Go Code: ```go func multiply(a, b float64) float64 { return a * b } func main() { tmpl := template.New("example").Funcs(template.FuncMap{ "multiply": multiply, }) tmpl, _ = tmpl.Parse(` Custom Function Example

    Price: ${{ .Price | multiply 1.1 }}

    `) data := struct { Price float64 }{ Price: 100.0, } tmpl.Execute(os.Stdout, data) } ``` ### Conclusion Go templates use `{{ ... }}` to perform various actions, including accessing variables, calling functions, iterating over collections, and evaluating conditions. This flexibility allows you to create dynamic, data-driven content easily. Understanding these actions helps you utilize the full power of Go templates effectively.