无法在 html 中显示结构切片

我正在开发一个需要我检索数据并将其显示在 html 上的项目。

数据被存储并作为结构切片传递给 html。

数据由不同的字段组成(例如名称、温度、湿度......)数据检索部分正在工作(我通过打印出来并在我的 html 上显示 Go 切片来确认)。但是,我想要实现的是根据字段在表中正确显示数据。我附上了我的代码,没有任何输出。我找不到错误。

请帮我解决一下这个。谢谢

*PS 所有名称/标签拼写正确。

http://img1.mukewang.com/6267dccf0001f11025590526.jpg

这是代码:


<div style="overflow:auto;">

        {{if .}}


        <table class="table table-striped">

                <thead>

                  <tr style="text-align: center;">

                    <th scope="col">Time</th>

                    <th scope="col">Name</th>

                    <th scope="col">Temperature</th>

                    <th scope="col">Humidity</th>

                    <th scope="col">Ambient</th>

                    <th scope="col">Red</th>

                    <th scope="col">Green</th>

                    <th scope="col">Blue</th>

                  </tr>

                </thead>

                <tbody>




        {{range .}}


        <tr style="text-align: center;">


          <td>{{.time_added}}</td>

          <td>{{.name}}</td>

          <td>{{.temperature }}</td>

          <td>{{.humidity }}</td>

          <td>{{.ambient }}</td>

          <td>{{.red }}</td>

          <td>{{.green}}</td>

          <td>{{.blue}}</td>

        </tr>

        {{end}}

    </tbody>


  </table>

    {{else}}

        <h1 style="font-size: 3vh;margin-top:10vh;">No data available</h1>

    {{end}}




  </div>



largeQ
浏览 144回答 1
1回答

拉丁的传说

最有可能的问题是您使用小写名称引用结构的字段。在 Go 中,小写是私有的,因此模板引擎无法看到它们。这是一个简单的工作示例:包主import (&nbsp; &nbsp; `html/template`&nbsp; &nbsp; `os`)var T = template.Must(template.New(``).Parse(`&nbsp; &nbsp; &nbsp; &nbsp; {{if .}}&nbsp; &nbsp; &nbsp; &nbsp; <table class="table table-striped">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr style="text-align: center;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Time</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; {{range .}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr style="text-align: center;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{.Time_Added}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{.Name}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{end}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; {{else}}&nbsp; &nbsp; &nbsp; &nbsp; <h1 style="font-size: 3vh;margin-top:10vh;">No data available</h1>&nbsp; &nbsp; {{end}}`))type S struct {&nbsp; &nbsp; Time_Added string&nbsp; &nbsp; Name string}func main() {&nbsp; &nbsp; data := []*S{&nbsp; &nbsp; &nbsp; &nbsp; &S{"today","fred"}, &S{"yesterday","joe"},&nbsp; &nbsp; }&nbsp; &nbsp; if err := T.Execute(os.Stdout, data); nil!=err {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }}如果您更改为.time_addedor .name,您将收到错误消息。因此,请务必检查来自T.Execute.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go