去模板绑定常量数组值

go template binding constant array values

我对go模板很陌生;我能知道如何用一些常量值绑定数组吗

我尝试过以下选项;但没用

{{ $groups := {"a", "b", "c"} }}
{{ $groups := ["a", "b", "c"] }}
{{ $groups := ("a", "b", "c") }}

模板不支持数组或切片的复合文字语法。

您可以使用 custom template function returns 其可变参数作为切片。

函数如下:

func slice(v ...interface{}) []interface{} {
  return v
}

在解析之前将函数添加到模板的映射中:

 template.New("").Funcs(template.FuncMap{"slice": slice}).Parse(data)

这样使用:

  {{$groups := slice "a" "b" "c"}}

working example on the playground