Skip to content

text/template: index is too restrictive in its type validation #15974

@bep

Description

@bep

Given:

package main

import (
    "html/template"
    "os"
)

func main() {
    m := make(map[template.HTML]string)
    m["Key"] = "Val"

    // OK!
    if _, ok := m["Key"]; !ok {
        panic("Map lookup failed")
    }

    t, err := template.New("foo").Parse(data)
    if err != nil {
        panic(err)
    }

    // This fails
    err = t.Execute(os.Stdout, m)

    if err != nil {
        panic(err)
    }
}

var data = `{{ index . "Key" }}`

This panics:

panic: template: foo:1:3: executing "foo" at <index . "Key">: error calling index: value has type string; should be template.HTML

Juggling around with template.HTML and string types seems like a common thing in Go templates, and I would expect the index func to work the same way as a straight map lookup in this case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions