mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
text/template: use sync.OnceValue for builtinFuncs
Replaced sync.Once with sync.OnceValue to simplify code and reduce globals.
Change-Id: I0586df379b855950eacc5b98baad68f6ba0ba129
GitHub-Last-Rev: 7540b1efba
GitHub-Pull-Request: golang/go#73689
Reviewed-on: https://go-review.googlesource.com/c/go/+/672235
Reviewed-by: Cherry Mui <cherryyz@google.com>
Auto-Submit: Sean Liao <sean@liao.dev>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Sean Liao <sean@liao.dev>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
parent
0201524c52
commit
777d76c4f2
1 changed files with 5 additions and 18 deletions
|
|
@ -62,26 +62,13 @@ func builtins() FuncMap {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var builtinFuncsOnce struct {
|
// builtinFuncs lazily computes & caches the builtinFuncs map.
|
||||||
sync.Once
|
var builtinFuncs = sync.OnceValue(func() map[string]reflect.Value {
|
||||||
v map[string]reflect.Value
|
funcMap := builtins()
|
||||||
}
|
m := make(map[string]reflect.Value, len(funcMap))
|
||||||
|
|
||||||
// builtinFuncsOnce lazily computes & caches the builtinFuncs map.
|
|
||||||
// TODO: revert this back to a global map once golang.org/issue/2559 is fixed.
|
|
||||||
func builtinFuncs() map[string]reflect.Value {
|
|
||||||
builtinFuncsOnce.Do(func() {
|
|
||||||
builtinFuncsOnce.v = createValueFuncs(builtins())
|
|
||||||
})
|
|
||||||
return builtinFuncsOnce.v
|
|
||||||
}
|
|
||||||
|
|
||||||
// createValueFuncs turns a FuncMap into a map[string]reflect.Value
|
|
||||||
func createValueFuncs(funcMap FuncMap) map[string]reflect.Value {
|
|
||||||
m := make(map[string]reflect.Value)
|
|
||||||
addValueFuncs(m, funcMap)
|
addValueFuncs(m, funcMap)
|
||||||
return m
|
return m
|
||||||
}
|
})
|
||||||
|
|
||||||
// addValueFuncs adds to values the functions in funcs, converting them to reflect.Values.
|
// addValueFuncs adds to values the functions in funcs, converting them to reflect.Values.
|
||||||
func addValueFuncs(out map[string]reflect.Value, in FuncMap) {
|
func addValueFuncs(out map[string]reflect.Value, in FuncMap) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue