多路等式测试失败

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

Multiway equality test failing

问题

所以我在Go模板中有以下的等式测试:

{{if eq .user.Role "Manager" "Admin"}}

根据文档1.2版本发布说明,这应该是有效的,但是我运气不好。

为了更清楚,.user.Role "Manager" 是有效的,或者任何只涉及一个参数的等式测试都是有效的。所以像这样的代码 {{if eq 1 1}} 可以正确评估,但是 {{if eq 1 2 1}} 不行。

我得到的错误是:3: 在执行 ".../index.html" 时出错,eq 的参数数量错误:期望 2 个,得到 3 个

英文:

So I have the following equality test in a go template

<code>{{if eq .user.Role "Manager" "Admin"}}</code>

which should work according to the docs and the 1.2 release notes, but I'm not having any luck

To make it more clear .user.Role "Manager" would work, or any equality test that involves only one argument. So something like
<code>{{if eq 1 1}}</code> evaluates correctly, but <code>{{if eq 1 2 1}}</code> does not.

Error I get is <code>3: executing ".../index.html" at <eq>: wrong number of args for eq: want 2 got 3</code>

答案1

得分: 1

好的,以下是翻译好的内容:

好的,最终的解决方案是取消注释 template.go 中 Revel 框架的 eq 实现部分。

英文:

Ok so the final solution was to uncomment the eq implementation that revel has in template.go.

答案2

得分: 0

你需要导出userUser)以使其可访问。

英文:

http://golang.org/src/pkg/text/template/exec_test.go#L71

You would need to export user (User) to make it accessible.

答案3

得分: 0

我可以想到两种可能性。要么你不是在1.2版本上(go version命令输出什么?),要么在模板的FuncMap中,eq函数被替换了。不幸的是,它没有被导出,所以我无法想到一个好的方法来确定是否是后者的情况。如果你没有替换它,你是否在使用修改了管道的包?如果是这样,请尝试在没有任何包的情况下进行渲染,看看是否可以工作。

英文:

I can think of two possibilities. Either you aren't on version 1.2 (what does go version give?) or somewhere in the pipeline the eq function was replaced in the template's FuncMap. Unfortunately, it's not exported, so I can't think of a good way to know if the latter is the case. If you haven't replaced it, are you using a package that modifies the pipeline? If so, try the rendering without any packages and see if it works.

huangapple
  • 本文由 发表于 2014年1月22日 19:36:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/21281837.html
匿名

发表评论

匿名网友

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

确定