go - Golang函数'无法评估字符串类型中的字段'
问题描述
我有一个 Item 类型的结构,其中包含 ItemFields ,它是字符串类型的切片。我想有条件地打印 ItemFields 中的每个字符串,它是带有锚标记的超链接。为此,我使用了一个 IsHyperlink 函数来检查切片中的每个字符串是应该包含在锚标记中还是简单地打印出来。
type Item struct {
ItemFields []string
}
我像这样在我的 page.html 中循环遍历 ItemFields。
{{range .Items}}
<ul>
<li>
{{range .ItemFields}}
{{if .IsHyperlink .}}
<a href="{{.}}">{{.}}</a>
{{else}}
{{.}}
{{end}}
{{end}}
</li>
</ul>
{{end}}
但是,当我运行应用程序 IsHyperlink 时报告它“无法评估字符串类型中的字段 IsHyperlink。
如何更改我的 go 代码以成功地将超链接包装在锚标记中?
解决方案
该上下文中的值.
是一个字符串,而不是Item
. 使用变量来引用项目:
{{range $item := .Items}}
<tr>
<td>
{{range .ItemFields}}
{{if $item.IsHyperlink .}}
<a href="{{.}}">{{.}}</a>
{{else}}
{{.}}
{{end}}
{{end}}
</td>
</tr>
{{end}}
推荐阅读
- c - leet代码问题2(加两个数字)使用malloc时出现运行时错误
- gnuplot - Gnuplot,具有不同调色板的多个splots
- javascript - 如何在 PHP/NodeJS 中将可编辑的 PDF 转换为不可编辑的 PDF?
- php - 如何在 Laravel 中运行一个独立的 PHP 脚本?
- typescript - 如何通过 Cloud Functions HTTP 请求检查 Firebase 规则用户权限
- android - 安装 Cordova 和 ionic 的问题
- xslt - 将日期从时区转换为 UTC XSLT 1.0
- laravel - 在 laravel 中获得多对多关系计数的最佳有效方法是什么?
- ruby-on-rails - Ruby / Rails:如何以清晰的方式格式化这种密集的数据结构?
- php - 为什么我的 curl 请求显示的结果与浏览器不同?