1 回答

TA贡献1789条经验 获得超8个赞
最有可能的问题是您使用小写名称引用结构的字段。在 Go 中,小写是私有的,因此模板引擎无法看到它们。
这是一个简单的工作示例:
包主
import (
`html/template`
`os`
)
var T = template.Must(template.New(``).Parse(`
{{if .}}
<table class="table table-striped">
<thead>
<tr style="text-align: center;">
<th scope="col">Time</th>
<th scope="col">Name</th>
</tr>
</thead>
<tbody>
{{range .}}
<tr style="text-align: center;">
<td>{{.Time_Added}}</td>
<td>{{.Name}}</td>
</tr>
{{end}}
</tbody>
</table>
{{else}}
<h1 style="font-size: 3vh;margin-top:10vh;">No data available</h1>
{{end}}
`))
type S struct {
Time_Added string
Name string
}
func main() {
data := []*S{
&S{"today","fred"}, &S{"yesterday","joe"},
}
if err := T.Execute(os.Stdout, data); nil!=err {
panic(err)
}
}
如果您更改为.time_addedor .name,您将收到错误消息。因此,请务必检查来自T.Execute.
- 1 回答
- 0 关注
- 115 浏览
添加回答
举报