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 微信公众号如何开发网页
  • ¥15 h3.6m 人类行为预测论文复现
  • ¥50 wordpress项目注册报失败刷新后其实是成功状态,请求排查原因
  • ¥20 linxu服务器僵尸进程不释放,代码如何修改?
  • ¥15 pycharm激活不成功
  • ¥40 如果update 一个列名为参数的value
  • ¥15 基于51单片机的水位检测系统设计中LCD1602一直不显示
  • ¥15 OCS2安装出现问题,请大家给点意见
  • ¥15 ros小车启动launch文件报错
  • ¥15 vs2015到期想登陆但是登陆不上