1

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") }}

1 Answer 1

3

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"}}

working example on the playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.