Skip to content

text/template: Functions are evaluated immediately and #16125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aarondl opened this issue Jun 20, 2016 · 1 comment
Closed

text/template: Functions are evaluated immediately and #16125

aarondl opened this issue Jun 20, 2016 · 1 comment

Comments

@aarondl
Copy link

aarondl commented Jun 20, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go version 1.6.2 linux/amd64
  2. What operating system and processor architecture are you using (go env)?
    Ubuntu 16.04 Xenial - Linux, amd64
  3. What did you do?
    If possible, provide a recipe for reproducing the error.
    A complete runnable program is good.
    A link on play.golang.org is best.
    https://play.golang.org/p/zbRIyl8apl
  4. What did you expect to see?
    I expected the function toupper to be passed as an argument to map and for the output of the program to be like this one https://play.golang.org/p/S8LsA-o5KX
  5. What did you see instead?
template: :1:10: executing "" at <toupper>: wrong number of args for toupper: want 1 got 0

It seems like toupper is evaluated as soon as it's seen, which breaks because it's meant to be an argument to the map function.

@adg
Copy link
Contributor

adg commented Jun 20, 2016

That's how template functions work. When they appear by name, they are invoked. They can't be passed as values within templates.

You can, however, pass the function as data, as you can see in this edited version of your first example:
https://play.golang.org/p/9BXHAwda14

@adg adg closed this as completed Jun 20, 2016
@golang golang locked and limited conversation to collaborators Jun 20, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants