Skip to content

Latest commit

 

History

History

001_conditional_logic_in_templates

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Go Sample Example - Conditional Logic in Templates

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.

📖 Information

  • 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.

💻 Code Example

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)
	}
}

🏃 How to Run

  1. Make sure you have Go installed. If not, you can download it from here.
  2. Clone this repository:
   git clone https://github.com/Rapter1990/go_sample_examples.git
  1. Navigate to the 029_text_samples/001_conditional_logic_in_templates directory:
   cd go_sample_examples/029_text_samples/001_conditional_logic_in_templates
  1. Run the Go program:
   go run 001_conditional_logic_in_templates.go

📦 Output

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.