切片结构到未排序的列表,留着小胡子

I have a struct.

type DataKey struct {
    Id        int64     `db:"id"`
    UserId    string    `db:"user_id"`
    Data      string    `db:"data"`
    CreatedAt time.Time `db:"created_at"`
}

I create a slice of structs.

data := []DataKey{}

After doing a sql query and filling the slices I try to pass to mustache to build my list.

mustache.RenderFileInLayout("templates/datakeys.html.mustache", "templates/layout.html.mustache", user, data)))

datakeys.html.mustache

<table class="table table-striped">
<thead>
    <tr>
        <th>#</th>
        <th>UserID</th>
        <th>DataKey</th>
        <th>CreatedAt</th>
    </tr>
</thead>
{{#DataKey}}
    <tr>
        <td>{{Id}}</td>
        <td>{{UserId}}</td>
        <td>{{Data}}</td>
        <td>{{CreatedAt}}</td>
    </tr>
{{/DataKey}}
</table>

The only thing I get is the table header. This function does not return an error so I don't know why it didn't like the data. I have also tried passing it in as a reference.

Im not familiar with mustache but from looking at it I think the {{#DataKey}} is wrong.

From the docs:

Template:

{{#repo}}
  <b>{{name}}</b>
{{/repo}}

Hash:

{
  "repo": [
    { "name": "resque" },
    { "name": "hub" },
    { "name": "rip" }
  ]
}

Output:

<b>resque</b>
<b>hub</b>
<b>rip</b>

I'd suggest trying the following

viewModel := struct{
    items []DataKey{}
}{
    data
}

mustache.RenderFileInLayout("templates/datakeys.html.mustache", "templates/layout.html.mustache", user, viewModel )))

and then replace the template with

{{#items}}
    <tr>
        <td>{{Id}}</td>
        <td>{{UserId}}</td>
        <td>{{Data}}</td>
        <td>{{CreatedAt}}</td>
    </tr>
{{/items}}

This is untested and might not be correct, but might be worth trying. My guess is that DataKey isn't a property on the model hence it's failing to evaluate.

Edit for more clarity: In theory

viewModel := struct{
    items []DataKey{}
}{
    data
}

will become

{
    "items": [
        {...},{...} ... etc
    ]
}