Golang无法更改模板变量的值。

huangapple go评论78阅读模式
英文:

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}}

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

答案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.

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

huangapple
  • 本文由 发表于 2014年7月21日 16:09:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/24860360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定