Standard library / html/template

html/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) error

Parameters & Attributes

NameTypeDescription
wrio.WriterWriter that receives the rendered output.
namestringName of the template.
dataanyInput data made available to the template.

Return Value

error

Parsing or setting error; nil on success.

Working Example

Example Snippetgo
package main
import (  "fmt"  "html/template"  "os")
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()}
Go Reference