text/template, html/template: document calling Funcs after parsing

Funcs was documented only as "It must be called before the template is
parsed", which suggests it cannot be called afterward. In fact a
function used in the template must be registered before parsing, but
Funcs may be called again later (including after Clone) to replace a
function of the same name, and the replacement takes effect when the
template is executed.

Fixes #34680

Change-Id: I819bc3bcb7452b2fd0bd08d2e97e737493944029
GitHub-Last-Rev: a521e2c82a9e8f3ed09c3ec5bcbf98f33b0d1933
GitHub-Pull-Request: golang/go#80356
Reviewed-on: https://go-review.googlesource.com/c/go/+/799340
Reviewed-by: Sean Liao <sean@liao.dev>
Reviewed-by: David Chase <drchase@google.com>
Auto-Submit: Sean Liao <sean@liao.dev>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Julian Soreavis 2026-07-10 14:33:38 +00:00 committed by Gopher Robot
parent ba26848a8e
commit 92efe23bc0
2 changed files with 10 additions and 6 deletions

View File

@ -331,10 +331,12 @@ func (t *Template) Name() string {
type FuncMap = template.FuncMap
// Funcs adds the elements of the argument map to the template's function map.
// It must be called before the template is parsed.
// Any function used in the template must be added before the template is
// parsed. Funcs may be called more than once, including after parsing (for
// example, after [Template.Clone]), to replace a function of the same name;
// the replacement is used when the template is executed.
// It panics if a value in the map is not a function with appropriate return
// type. However, it is legal to overwrite elements of the map. The return
// value is the template, so calls can be chained.
// type. The return value is the template, so calls can be chained.
func (t *Template) Funcs(funcMap FuncMap) *Template {
t.text.Funcs(template.FuncMap(funcMap))
return t

View File

@ -167,11 +167,13 @@ func (t *Template) Delims(left, right string) *Template {
}
// Funcs adds the elements of the argument map to the template's function map.
// It must be called before the template is parsed.
// Any function used in the template must be added before the template is
// parsed. Funcs may be called more than once, including after parsing (for
// example, after [Template.Clone]), to replace a function of the same name;
// the replacement is used when the template is executed.
// It panics if a value in the map is not a function with appropriate return
// type or if the name cannot be used syntactically as a function in a template.
// It is legal to overwrite elements of the map. The return value is the template,
// so calls can be chained.
// The return value is the template, so calls can be chained.
func (t *Template) Funcs(funcMap FuncMap) *Template {
t.init()
t.muFuncs.Lock()