Standard library / text/template/parse
text/template/parse.TemplateNode.Copy()
Copy returns a node with the same syntax. Use the concrete copy for independent tree editing; this example checks syntax and node identity, not every field. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func (t *TemplateNode) Copy() NodeReturn Value
Node
Copy returns a node with the same syntax.
Working Example
Example Snippetgo
package main
import ( "fmt" "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("{{template \"header\" .}}") n := t.Root.Nodes[0].(*parse.TemplateNode) c := n.Copy() fmt.Println(c.String() == n.String() && c != n)}