text/template: provide example of overwriting template func after parse
This example illustrates how to overwrite a template function after parsing a template.
This example is intended to clarify the point made in the template.Funcs docstring
that "[i]t is legal to overwrite elements of the map."
diff --git a/src/text/template/examplefunc_test.go b/src/text/template/examplefunc_test.go
index 080b5e3..bf395fb 100644
--- a/src/text/template/examplefunc_test.go
+++ b/src/text/template/examplefunc_test.go
@@ -52,3 +52,47 @@
// Output 1: "The Go Programming Language"
// Output 2: "The Go Programming Language"
}
+
+// This example demonstrates registering two custom template functions
+// and how to overwite one of the functions after the template has been
+// parsed. Overwriting can be used, for example, to alter the operation
+// of cloned templates.
+func ExampleTemplate_funcs() {
+
+ // Define a simple template to test the functions.
+ const tmpl = `{{ . | lc | rpt }}`
+
+ // Define the template funcMap with two functions.
+ var funcMap = template.FuncMap{
+ "rpt": func(s string) string { return strings.Repeat(s, 2) },
+ "lc": strings.ToLower,
+ }
+
+ // Define a New template, add the funcMap using Funcs and then Parse
+ // the template.
+ parsedTmpl, err := template.New("t").Funcs(funcMap).Parse(tmpl)
+ if err != nil {
+ log.Fatal(err)
+ }
+ if err := parsedTmpl.Execute(os.Stdout, "ABC\n"); err != nil {
+ log.Fatal(err)
+ }
+
+ // [Funcs] must be called before a template is parsed to add
+ // functions to the template. [Funcs] can also be used after a
+ // template is parsed to overwrite template functions.
+ //
+ // Here the function identified by "rpt" is overwritten.
+ parsedTmpl.Funcs(template.FuncMap{
+ "rpt": func(s string) string { return strings.Repeat(s, 3) },
+ })
+ if err := parsedTmpl.Execute(os.Stdout, "DEF\n"); err != nil {
+ log.Fatal(err)
+ }
+ // Output:
+ // abc
+ // abc
+ // def
+ // def
+ // def
+}
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
I spotted some possible problems.
These findings are based on simple heuristics. If a finding appears wrong, briefly reply here saying so. Otherwise, please address any problems and update the GitHub PR. When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above.
Possible problems detected:
1. Lines in the commit message should be wrapped at ~76 characters unless needed for things like URLs or tables. You have a 87 character line.
The commit title and commit message body come from the GitHub PR title and description, and must be edited in the GitHub web interface (not via git). For instructions, see [here](https://go.dev/wiki/GerritBot/#how-does-gerritbot-determine-the-final-commit-message). For guidelines on commit messages for the Go project, see [here](https://go.dev/doc/contribute#commit_messages).
(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Congratulations on opening your first change. Thank you for your contribution!
Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.
Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.
During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11 or adds a tag like "wait-release", it means that this CL will be
reviewed as part of the next development cycle. See https://go.dev/s/release
for more details.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
I spotted some possible problems.
These findings are based on simple heuristics. If a finding appears wrong, briefly reply here saying so. Otherwise, please address any problems and update the GitHub PR. When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above.
Possible problems detected:
1. Lines in the commit message should be wrapped at ~76 characters unless needed for things like URLs or tables. You have a 87 character line.The commit title and commit message body come from the GitHub PR title and description, and must be edited in the GitHub web interface (not via git). For instructions, see [here](https://go.dev/wiki/GerritBot/#how-does-gerritbot-determine-the-final-commit-message). For guidelines on commit messages for the Go project, see [here](https://go.dev/doc/contribute#commit_messages).
(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)
Acknowledged
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
const tmpl = `{{ . | lc | rpt }}`I think the example will be a bit clearer with longer names. How about
{{ . | lower | repeat }}| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Commit-Queue | +1 |
I think the example will be a bit clearer with longer names. How about
{{ . | lower | repeat }}
Done
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Auto-Submit | +1 |
| Code-Review | +2 |
Thanks.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Code-Review | +1 |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
text/template: provide example of overwriting template func after parse
This example illustrates how to overwrite a template function after parsing a template.
This example is intended to clarify the point made in the template.Funcs docstring
that "[i]t is legal to overwrite elements of the map."
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |