Standard library / text/template/parse
text/template/parse.Tree.ErrorContext()
Returns source location and syntax context for a node. Positions are byte offsets. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func (t *Tree) ErrorContext(n Node) (location, context string)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| n | Node | Input to the operation as described above. |
Return Value
(location, context string)
Returns source location and syntax context for a node.
Working Example
Example Snippetgo
package main
import ( "fmt" "strings" "text/template/parse")
func tree(s string) *parse.Tree { t := parse.New("demo") t.Mode = parse.ParseComments | parse.SkipFuncCheck _, e := t.Parse(s, "", "", map[string]*parse.Tree{}) if e != nil { panic(e) } return t}func action(s string) *parse.ActionNode { return tree(s).Root.Nodes[0].(*parse.ActionNode) }
func main() { t := tree("Hi {{.Name}}") loc, ctx := t.ErrorContext(t.Root.Nodes[1]) fmt.Println(strings.HasPrefix(loc, "demo:") && ctx == "{{.Name}}")}