I am very new to go template; Can I know how I can bind an array with some constant values
I had tried below options; but not worked
{{ $groups := {"a", "b", "c"} }}
{{ $groups := ["a", "b", "c"] }}
{{ $groups := ("a", "b", "c") }}
I am very new to go template; Can I know how I can bind an array with some constant values
I had tried below options; but not worked
{{ $groups := {"a", "b", "c"} }}
{{ $groups := ["a", "b", "c"] }}
{{ $groups := ("a", "b", "c") }}
Templates do not support composite literal syntax for arrays or slices.
You can use a custom template function that returns its variadic arguments as a slice.
Here's the function:
func slice(v ...interface{}) []interface{} {
return v
}
Add the function to the template's map before parsing:
template.New("").Funcs(template.FuncMap{"slice": slice}).Parse(data)
Use it like this:
{{$groups := slice "a" "b" "c"}}
<kbd>working example on the playground</kbd>