This repository demonstrates the use of conditional logic in Go's text/template
package. It shows how to handle conditions within templates to control the flow of content based on dynamic data, such as struct fields.
- This example covers basic conditional logic in Go templates.
- It includes a demonstration of an `if` block to check the presence of a field and display alternate content if the condition is not met.
- Shows how to render templates dynamically with struct data.
package main
import (
"fmt"
"os"
"text/template"
)
type Person struct {
Name string
Age int
Country string
}
func main() {
// This template includes a conditional if block to check whether the Age field is present.
// If it's not, an alternate message is shown
person := Person{
Name: "Alice",
Age: 25,
Country: "Canada",
}
tpl := `{{if .Age}}My name is {{.Name}} and I am {{.Age}} years old.{{else}}My name is {{.Name}} and I prefer not to disclose my age.{{end}}
I live in {{.Country}}.`
t := template.Must(template.New("person").Parse(tpl))
err := t.Execute(os.Stdout, person)
if err != nil {
fmt.Println("Error executing template:", err)
}
}
- Make sure you have Go installed. If not, you can download it from here.
- Clone this repository:
git clone https://github.com/Rapter1990/go_sample_examples.git
- Navigate to the
029_text_samples/001_conditional_logic_in_templates
directory:
cd go_sample_examples/029_text_samples/001_conditional_logic_in_templates
- Run the Go program:
go run 001_conditional_logic_in_templates.go
When you run the program, you should see the following output:
My name is Alice and I am 25 years old.
I live in Canada.
If the Age
field were missing or set to 0
, the template would display:
My name is Alice and I prefer not to disclose my age.
I live in Canada.