Skip to content

proposal: text/template: slice spread functionality in template func context #51674

@ddemoss222

Description

@ddemoss222

Problem: Using variadic functions within text/templates there presently not a clean way to drop a defined slice into that function. Attempts to do so result in:

wrong type for value. Expected string, got []string

Direct usage of the spread operator valid in a go code context leads to a syntax error within a template context:

unexpected <.> in operand

I can imagine two solutions to this problem. Either a "spread" builtin template function that can be dropped ahead of the struct to "spread" the a generic slice in a manner identical to the ellipsis notation but valid within a template context, or automatic type flexibility that allows variadic template funcs to process slices of the same type T as though they were valid variadic types.

Present Workaround:
Presently the only way I am aware of to work around this problem is to create a func wrapper.

// variadic func in question
func variadicFunc(s ...string) output {
   ....
}

func wrapperFunc(s []string) output {
   return variadicFunc(s...)
}

If there are better workarounds, let me know. Otherwise, my humble thanks for considering this feature request.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions