1

I have this code: http://play.golang.org/p/mPX1azLhlg but why I can't change my $foo value? How I supposed to do this?

1

2 Answers 2

3

This seems to have been updated in go1.11: https://golang.org/doc/go1.11#text/template

Modifying template variables via assignments is now permitted via the = token

So you need to change {{$foo := 1}} to {{$foo = 1}}

https://play.golang.org/p/hqWClmZfjcx

Sign up to request clarification or add additional context in comments.

Comments

2

A variable's scope extends to the "end" action of the control structure ("if", "with", or "range") in which it is declared, or to the end of the template if there is no such control structure.

(http://golang.org/pkg/text/template/)

2 Comments

how can I bypass this restriction?
You can define everything you need outside the template (in the go code), and then pass it inside.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.