Text/template call function

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!

1 Like

Maybe this tutorial will help:
Using Functions Inside Go Templates

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

1 Like

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

1 Like

You can check the docs in template package - text/template - Go Packages

1 Like

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

1 Like

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

1 Like

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

1 Like

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

1 Like

I stated, I don’t understand the docs = It does not explain enough to me. So please give some working example or something more specific. Thank you

1 Like

If there is a method Foo(int) in the context then just do call .Foo 1. That’s it. But that has already been written in the docs I cited. I’m not sure what more example you need?

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.