Text/template call function


(Martin “caubert” Kauber) #1

Hi! I have difficulties understanding template package’s predefined function named “call”. When to use it and also how to use it. Are there any examples. Reading the docs on call does not help me much.

Thank you!


( Kvaz1r) #2

Maybe this tutorial will help:
Using Functions Inside Go Templates

It’s about html templates but as far I know they are the same.


(Martin “caubert” Kauber) #3

yup, I’ve seen this, but that article does not explain anything… sadly just “type this there and done”


(Yamil Bracho) #4

You can check the docs in https://godoc.org/text/template


(Norbert Melzer) #5

Do you mean this?

call
Returns the result of calling the first argument, which
must be a function, with the remaining arguments as parameters.
Thus “call .X.Y 1 2” is, in Go notation, dot.X.Y(1, 2) where
Y is a func-valued field, map entry, or the like.
The first argument must be the result of an evaluation
that yields a value of function type (as distinct from
a predefined function such as print). The function must
return either one or two result values, the second of which
is of type error. If the arguments don’t match the function
or the returned error value is non-nil, execution stops


(Martin “caubert” Kauber) #6

as I stated, I’ve read the docs and it does not help much understanding how call works


(Yamil Bracho) #7

Yes, that is the only refrence in the docs and not sample :frowning:


(Norbert Melzer) #8

It explains enough. And even has a small example… It calls the qualified method on the context, passing the space separated values as arguments.