Standard library / text/template/parse
text/template/parse.Tree.Copy()
Copies the tree root and discards parsing state. Go 1.23.2 does not preserve Mode; do not rely on the copy for reparsing with the same configuration. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func (t *Tree) Copy() *TreeReturn Value
*Tree
Copies the tree root and discards parsing state.
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("Hi") c := t.Copy() fmt.Println(c != t && c.Root != t.Root && c.Root.String() == t.Root.String() && c.Mode == 0)}