英文:
Golang can't change template variable value
问题
我有这段代码:http://play.golang.org/p/mPX1azLhlg,但为什么我不能改变我的$foo
的值?我应该怎么做?
英文:
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
得分: 3
这似乎在go1.11中已经更新了:
https://golang.org/doc/go1.11#text/template
> 现在允许通过赋值来修改模板变量,使用 = 符号
所以你需要将 {{$foo := 1}}
改为 {{$foo = 1}}
https://play.golang.org/p/hqWClmZfjcx
英文:
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}}
答案2
得分: 2
一个变量的作用域延伸到声明它的控制结构("if"、"with"或"range")的"end"动作,或者如果没有这样的控制结构,则延伸到模板的末尾。
(http://golang.org/pkg/text/template/)
英文:
> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论