Golang 函数“无法评估字符串类型的字段”
Golang function 'can't evaluate field in type string'
我有一个 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}}
我有一个 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}}