struct field name conflicts with function in template

polaris · · 328 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Using a struct with a field name that matches a template function (I think) causes the template to raise an error during rendering. Is there a way to display the attribute other that renaming it?</p> <p>Example struct:</p> <pre><code>type MyObject struct { ObjectId int `form:&#34;objectId&#34; json:&#34;objectId&#34;` Type string `form:&#34;type&#34; json:&#34;type&#34;` } </code></pre> <p>Example template:</p> <pre><code>&lt;div&gt;Objects:&lt;/div&gt; &lt;ul&gt; {# range $obj := .Objects #} &lt;li&gt;{# $obj.Type #} - ({# $obj.ObjectId #})&lt;/li&gt; {# end #} &lt;/ul&gt; </code></pre> <p>Edit: including error output by template</p> <pre><code>template: example.tpl:231:58: executing &#34;content&#34; at &lt;$obj.Type&gt;: Type has arguments but cannot be invoked as function </code></pre> <hr/>**评论:**<br/><br/>hipone: <pre><p>Works for me.</p> <p><a href="https://play.golang.org/p/KaxuIPhiEa" rel="nofollow">https://play.golang.org/p/KaxuIPhiEa</a></p></pre>mbondfusion: <pre><p>Thanks...I see my mistake now. I had this:</p> <pre><code>{{ $obj.Type ($obj.ObjectId) }} </code></pre> <p>Instead of this:</p> <pre><code>{{ $obj.Type }} ({{ $obj.ObjectId }}) </code></pre> <p>..Not enough brackets (and thanks for not pointing out my custom brackets :). I use Handlebars along with golang and it uses the same brackets, so I use custom golang template tokens.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

328 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传