Standard library / text/template/parse

text/template/parse.Parse()

Parses trusted source into a map of named trees; supply known function names in function maps. No rendering occurs. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func Parse(name, text, leftDelim, rightDelim string, funcs ...map[string]any) (map[string]*Tree, error)

Parameters & Attributes

NameTypeDescription
namestringName of the template.
textstringTemplate source text, authored by a trusted developer.
leftDelimstringInput to the operation as described above.
rightDelimstringInput to the operation as described above.
funcs...map[string]anyInput to the operation as described above.

Return Value

(map[string]*Tree, error)

Parses trusted source into a map of named trees; supply known function names in function maps.

Working Example

Example Snippetgo
package main
import (  "fmt"  "text/template/parse")
func check(e error) {  if e != nil {    panic(e)  }}
func main() {  ts, e := parse.Parse("demo", `{{define "header"}}Hi{{end}}{{template "header"}}`, "", "")  check(e)  fmt.Println(len(ts) == 2 && ts["header"].Root.String() == "Hi")}
Go Reference