gpt4 book ai didi

html - Go: template.ParseFiles() 不适用于 {{.active}} 但适用于 {{printf "%s".active}}

转载 作者:数据小太阳 更新时间:2023-10-29 03:16:53 26 4
gpt4 key购买 nike

让我这样说我的 html 文件的主体

<body>
<h2>Current number of players: {{.active}}</h2>
</body>

我的代码看起来像

type page struct{
active string
}
t, _ template.ParseFiles("page.html")
t.Execute(w,page{active: "No Players are Online"})

当我运行代码时,出现空白屏幕。当我将 {{.active}} 更改为 {{printf "%s".active}} 它有效。

我总是需要包含 printf 吗?我想我对文档感到困惑。

谢谢!

最佳答案

使 active 属性大写。像这样:

type page struct{
Active string
}
t, _ template.ParseFiles("page.html")
t.Execute(w,page{Active: "No Players are Online"})

和模板

<body>
<h2>Current number of players: {{.Active}}</h2>
</body>

Go 仅将大写结构属性导出到其他模块。

关于html - Go: template.ParseFiles() 不适用于 {{.active}} 但适用于 {{printf "%s".active}},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30304320/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com