GoLang在模板索引中出现了问题。

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

GoLang hangs in template indexing

问题

我正在尝试使用以下模板填充表格:

  1. <table class="table">
  2. <tr>
  3. <td>Repo name</td>
  4. <td>Repo id</td>
  5. </tr>
  6. {{range $i, $e := .GitHubRepoNames}}
  7. <tr>
  8. <td>{{$e}}</td>
  9. <td>{{index .GitHubRepoNames $i}}</td>
  10. </tr>
  11. {{end}}
  12. </table>

当我执行这个模板时,输出结果为:

  1. <table class="table">
  2. <tr>
  3. <td>Repo name</td>
  4. <td>Repo id</td>
  5. </tr>
  6. <tr>
  7. <td>https://api.github.com/repos/ertemplin/cah/issues{/number}</td>
  8. <td>

当我在模板中去掉{{index}}调用时:

  1. <table class="table">
  2. <tr>
  3. <td>Repo name</td>
  4. <td>Repo id</td>
  5. </tr>
  6. {{range $i, $e := .GitHubRepoNames}}
  7. <tr>
  8. <td>{{$e}}</td>
  9. <td>{{$i}}</td>
  10. </tr>
  11. {{end}}
  12. </table>

它会输出完整的范围:

  1. <table class="table">
  2. <tr>
  3. <td>Repo name</td>
  4. <td>Repo id</td>
  5. </tr>
  6. <tr>
  7. <td>https://api.github.com/repos/ertemplin/cah/issues{/number}</td>
  8. <td>0</td>
  9. </tr>
  10. </table>

在模板的第一个实例中,可能是什么原因导致输出被中断?

英文:

I'm trying to fill in a table with the following template:

  1. &lt;table class=&quot;table&quot;&gt;
  2. &lt;tr&gt;
  3. &lt;td&gt;Repo name&lt;/td&gt;
  4. &lt;td&gt;Repo id&lt;/td&gt;
  5. &lt;/tr&gt;
  6. {{range $i, $e := .GitHubRepoNames}}
  7. &lt;tr&gt;
  8. &lt;td&gt;{{$e}}&lt;/td&gt;
  9. &lt;td&gt;{{index .GitHubRepoNames $i}}&lt;/td&gt;
  10. &lt;/tr&gt;
  11. {{end}}
  12. &lt;/table&gt;

When I execute this template, it outputs:

  1. &lt;table class=&quot;table&quot;&gt;
  2. &lt;tr&gt;
  3. &lt;td&gt;Repo name&lt;/td&gt;
  4. &lt;td&gt;Repo id&lt;/td&gt;
  5. &lt;/tr&gt;
  6. &lt;tr&gt;
  7. &lt;td&gt;https://api.github.com/repos/ertemplin/cah/issues{/number}&lt;/td&gt;
  8. &lt;td&gt;

When I run the template without the {{index}} call:

  1. &lt;table class=&quot;table&quot;&gt;
  2. &lt;tr&gt;
  3. &lt;td&gt;Repo name&lt;/td&gt;
  4. &lt;td&gt;Repo id&lt;/td&gt;
  5. &lt;/tr&gt;
  6. {{range $i, $e := .GitHubRepoNames}}
  7. &lt;tr&gt;
  8. &lt;td&gt;{{$e}}&lt;/td&gt;
  9. &lt;td&gt;{{$i}}&lt;/td&gt;
  10. &lt;/tr&gt;
  11. {{end}}
  12. &lt;/table&gt;

it outputs the complete range:

  1. &lt;table class=&quot;table&quot;&gt;
  2. &lt;tr&gt;
  3. &lt;td&gt;Repo name&lt;/td&gt;
  4. &lt;td&gt;Repo id&lt;/td&gt;
  5. &lt;/tr&gt;
  6. &lt;tr&gt;
  7. &lt;td&gt;https://api.github.com/repos/ertemplin/cah/issues{/number}&lt;/td&gt;
  8. &lt;td&gt;0&lt;/td&gt;
  9. &lt;/tr&gt;
  10. &lt;/table&gt;

What could be causing the output to be interrupted in the first instance of my template?

答案1

得分: 6

当执行模板时,会返回一个错误:

  1. var buf bytes.Buffer
  2. err := tpl.Execute(&buf, map[string]interface{}{
  3. "GitHubRepoNames": []string{
  4. "https://api.github.com/repos/ertemplin/cah/issues{/number}",
  5. },
  6. })
  7. fmt.Println(err, buf.String())

错误信息为:

template: ex:9:20: 在类型为字符串的字段中无法评估字段GitHubRepoNames

这意味着.被更改为$e。我不确定为什么需要这样做索引($e似乎足够了),但你可以这样做:

  1. <td>{{index $.GitHubRepoNames $i}}</td>

$在文档中有解释:

当执行开始时,$被设置为传递给Execute的数据参数,即dot的初始值。

英文:

When you execute a template an error is returned:

  1. var buf bytes.Buffer
  2. err := tpl.Execute(&amp;buf, map[string]interface{}{
  3. &quot;GitHubRepoNames&quot;: []string{
  4. &quot;https://api.github.com/repos/ertemplin/cah/issues{/number}&quot;,
  5. },
  6. })
  7. fmt.Println(err, buf.String())

The error is:

> template: ex:9:20: executing "ex" at <.GitHubRepoNames>: can't evaluate field GitHubRepoNames in type string

Which means the . is being changed to $e. I'm not sure why you need to do the index like this ($e seems like it ought to be sufficient) but you can do this:

  1. &lt;td&gt;{{index $.GitHubRepoNames $i}}&lt;/td&gt;

$ is explained by the documentation:

> When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.

huangapple
  • 本文由 发表于 2013年12月21日 14:59:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/20716471.html
匿名

发表评论

匿名网友

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

确定