Hi Carl,
try quicktemplate <https://github.com/valyala/quicktemplate> instead of
html/template. It supports arbitrary data transformations inside the
template code, so the 'totals' row may be easily implemented without
external code:
Suppose you have the following row struct:
type Row struct {
URL string
Hits int
TimeSum time.Duration
}
Then the template function generating HTML table for the given rows may
look like:
{% func TableWithTotals(rows []Row) %}
{% code
// declare ordinary Go variables inside template for calculating the
'totals' row.
var (
hitsSum int
timeSum time.Duration
)
// Arbitrary data transformations may be performed here via usual Go
code.
// For instance, rows may be sorted, filtered, aggregated and sliced
before outputting
// them in the template.
%}
<table>
<tr>
<th>#</th>
<th>URL</th>
<th>Hits</th>
<th>Avg request duration</th>
</tr>
{% for i, r := range rows %}
{% code
// increment totals
hitsSum += r.Hits
timeSum += r.TimeSum
%}
<tr>
<td>{%d i %}</td>
<td>{%s r.URL %}</td>
<td>{%d r.Hits %}</td>
<td>{%v r.TimeSum / time.Duration(r.Hits) %}</td>
</tr>
{% endfor %}
<tr>
<th>Totals</th>
<th></th>
<th>{%d hitsSum %}</th>
<th>{%v timeSum / time.Duration(hitsSum) %}</th>
</tr>
</table>
{% endfunc %}
On Friday, August 26, 2016 at 1:14:11 PM UTC+3, Carl Ranson wrote:
>
>
> Ok,
>
> Thanks for the answers. I've gone down the route of adding totals to my
> data structure.
>
> cheers,
> CR.
>
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.