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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失