doufangyan6862 2019-05-25 14:37
浏览 79
已采纳

用我的结构Golang从切片显示表格

I want to show a table that each row contains my struct data.

Here is my struct:

type My_Struct struct {
FIRST_FIELD       string
SECOND_FIELD      string
THIED_FIELD       string
}

Here is my html code:

<table id="t01">
<tr>
    <th>FIRST FIELD</th>
    <th>SECOND FIELD</th>
    <th>THIRD FIELD</th>
</tr>
<tr>
    <td>FIRST_OBJ_HERE_SHOULD_BE_THE_FIRST_FIELD</td>
    <td>FIRST_OBJ_HERE_SHOULD_BE_THE_SECOND_FIELD</td>
    <td>FIRST_OBJ_HERE_SHOULD_BE_THE_THIRD_FIELD</td>
</tr>

<tr>
    <td>SECOND_OBJ_HERE_SHOULD_BE_THE_FIRST_FIELD</td>
    <td>SECOND_OBJ_HERE_SHOULD_BE_THE_SECOND_FIELD</td>
    <td>SECOND_OBJ_HERE_SHOULD_BE_THE_THIRD_FIELD</td>
</tr>

</table>

As you see, I want to pass a slice with my struct (each one contains 3 files) to this html code, and I want the the whole slice will be set in this table - each row contains one struct data.

How can I achieve this?

Thank you!

  • 写回答

1条回答 默认 最新

  • duandian2725 2019-05-25 16:54
    关注

    It seems like you want the Go Template package.

    Here's an example of how you may use it: Define a handler that passes an instance of a struct with some defined field(s) to a view that uses Go Templates:

    type MyStruct struct {
            SomeField string
    }
    
    func MyStructHandler(w http.ResponseWriter, r *http.Request) {
            ms := MyStruct{
                    SomeField: "Hello Friends",
            }
    
            t := template.Must(template.ParseFiles("./showmystruct.html"))
    t.Execute(w, ms)
    }
    

    Access the struct fields using the Go Template syntax in your view (showmystruct.html):

    <!DOCTYPE html>
    <title>Show My Struct</title>
    <h1>{{ .SomeField }}</h1>
    

    Update

    If you are interested particularly in passing a list, and iterating over that, then the {{ range }} keyword is useful. Also, there's a pretty common pattern (at least in my world) where you pass a PageData{} struct to the view.

    Here's an expanded example, adding a list of structs, and a PageData struct (so we can access its fields in the template):

    type MyStruct struct {
        SomeField string
    }
    
    type PageData struct {
        Title string
        Data []MyStruct
    }
    
    func MyStructHandler(w http.ResponseWriter, r *http.Request) {
            data := PageData{
                Title: "My Super Awesome Page of Structs",
                Data: []MyStruct{
                    MyStruct{
                        SomeField: "Hello Friends",
                    },
                    MyStruct{
                        SomeField: "Goodbye Friends",
                    },
                }
    
            t := template.Must(template.ParseFiles("./showmystruct.html"))
            t.Execute(w, data)
    
    }
    

    And the modified template (showmystruct.html):

    <!DOCTYPE html>
    <title>{{ .Title }}</title>
    <ul>
      {{ range .Data }}
        <li>{{ .SomeField }}</li>
      {{ end }}
    </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵