如何使用 go 模板在 table 中生成超链接

How to generate hyperlinks in table using go template

我想生成一个 html table,它在一个 table 单元格中包含一些超链接字符串。 例如,

<a href="http://logd/logs/2800643/">2800643</a> 
<a href="http://logd/logs/2800795/">2800795</a> 
<a href="http://logd/logs/2801109/">2801109</a> 

我已经定义了一个模板,它可以很好地处理 table 除了这个超链接之外的东西。

{{ range . }}
<tr>
<td>{{ .Name }}</td>
<td>{{ .Logs }}</td>
</tr>
{{ end }}

这里我想让日志超链接。 如果我在这里将格式化的字符串传递到日志中,< > 将被解析如下:

&lt;a href=&#34;http://logd/logs/2801103/&#34;&gt;2801103&lt;/a&gt

根据我从 go 网站上了解到的信息,看来我需要定义另一个嵌入到这个模板中的模板。 假设这里要替换{{ .Logs }}。喜欢:

{{ range . }}
<a href="http://logd/logs/{{.LogID}}/">{{.LogID}}</a>
{{ end }}

不知道有没有可用的例子?谢谢

html 有一个 html-template which uses type html:

HTML encapsulates a known safe HTML document fragment.

这意味着:

Based on the information I learned from go website, looks like I need to define another template embedded into this one. Suppose it's to replace {{ .Logs }} here.

不要使用字符串,而是在结构中使用 html 类型。