Standard library / text/template
text/template.Template.ExecuteTemplate()
Renders the named associated template rather than the receiver. Expected stdout: 'Hello Ada\n'.
Formal Syntax & Structure
go syntaxgo
func (t *Template) ExecuteTemplate(wr io.Writer, name string, data any) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| wr | io.Writer | Writer that receives the rendered output. |
| name | string | Name of the template. |
| data | any | Input data made available to the template. |
Return Value
error
Parsing or setting error; nil on success.
Working Example
Example Snippetgo
package main
import ( "fmt" "os" "text/template")
func check(e error) { if e != nil { panic(e) }}
func main() { t := template.Must(template.New("root").Parse(`{{define "greet"}}Hello {{.}}{{end}}`)) check(t.ExecuteTemplate(os.Stdout, "greet", "Ada")) fmt.Println()}