转到:template.ParseFiles()不适用于{{.active}},但适用于{{printf“%s” .active}}

Lets say the body of my html file like so

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

And my go code looks like

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

When I run the code, I get a blank screen. When I change {{.active}} to {{printf "%s" .active}} it works.

Do I always need to include printf? I guess I'm confused by the documentation.

Thanks!

Make active property capitalized. Like so:

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

and template

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

Go exports only capitalized struct properties to other modules.