package main import ( "fmt" "text/template" ) func main() { // define the template string templateString := "Hello {{.Name}}, your age is {{.Age}}\n" // create a new template t := template.Must(template.New("test").Parse(templateString)) // create a map of values to look up data := map[string]interface{}{ "Name": "Alice", "Age": 25, } // use the Lookup function to get the "Name" value from the map lookupVal := t.Lookup("Name") fmt.Println(lookupVal) // execute the template with the map of values err := t.Execute(os.Stdout, data) if err != nil { panic(err) } }
package main import ( "fmt" "text/template" ) func main() { // Define the template const tplString = "Hello {{.Name}}!" // Parameter data structure data := struct{ Name string }{"Alice"} // Parse the template tpl := template.Must(template.New("test").Parse(tplString)) // Execute the template if err := tpl.Execute(os.Stdout, data); err != nil { panic(err) } }In this example, we define a simple template that outputs a greeting message with the name passed as a parameter. We use the Lookup function implicitly in this case, as we use the data structure to pass the Name key-value pair. The template is executed with the data structure as a parameter, and the output is printed to the standard output.