I'm developing a Go! application using the html templating built-in library. In my template, the following works:
{{range .industries }}
...
<td style="text-align:right">{{ .SalesStock.Size }}</td>
...
{{ end }}
industries is a slice of objects of type Industry. SalesStock is a method of Industry and yields an object SalesStock of type Stock, which has a field Size.
This code works fine.
In place of Size I want to call a method that will display either the Size or the Price field, depending on a parameter mode.
The (simplified) method is
func (stock Stock) DisplaySize(mode string) float32 {
switch mode {
case `price`:
return stock.Size
case `size`:
return stock.Price
default:
panic("unknown display mode requested")
}
}
I replaced the line in the template with:
<td style="text-align:right">{{ call .SalesStock.DisplaySize "size" }}</td>
and received the error
at <.SalesStock.DisplaySize>: wrong number of args for DisplaySize: want 1 got 0
What did I do wrong?
thanks for any offers. I suspect the mistake is elementary. I'm new to Go.
The builtin
callis to call a function value. You want to call a method, simply removecalland it'll work:You would need
callifDisplaySizewould be a regular field (of function type) and you'd want to call that.For example:
This will output (try it on the Go Playground):