How do I use the predefined function "call" in a text / template?
I am trying to figure out how to use a function call
in a package text/template
. Here's an example:
type Human struct {
Name string
}
func (h *Human) Say(str string) string {
return str
}
func main() {
const letter = `
{{.Name}} wants to say {{"blabla" | .Say}}
{{.Name}} wants try again, {{call .Say "blabla"}}.`
var h = &Human{"Tim"}
t := template.Must(template.New("").Parse(letter))
err := t.Execute(os.Stdout, h)
if err != nil {
log.Println("executing template:", err)
}
}
see this code at play.golang.org
I thought I was call
calling functions / methods, but as it turns out, we can only do this with .Method arg1 arg2
. So what is the function for call
?
source to share
You need to use call
if you want to call the function value.
To quote the docs (see the Functions section):
Thus, "call .XY 1 2" is in Go notation, dot.XY (1, 2), where Y is a function field, a map entry, and so on.
This example X
might look like this:
type X struct {
Y func(a int, b int) int
}
source to share