Skip to content

text/template: type checker bug with missing map keys #3850

@remyoudompheng

Description

@remyoudompheng
What steps will reproduce the problem?
1. Compile and run the following program

package main

import (
      "bytes"
      "fmt"
      "math/big"
      "text/template"
)

func main() {
      n, _ := new(big.Int).SetString("123456789", 10)
      display := func(n *big.Int) string { return n.String() }
      tpl := template.Must(template.
            New("test").
            Funcs(template.FuncMap{"display": display}).
            Parse(`{{ index $.Values $.Key | display }}`))

      type data struct {
            Values map[string]*big.Int
            Key    string
      }

      values := map[string]*big.Int{"number": n, "hello": nil}

      for _, key := range []string{"number", "hello", "world"} {
            buf := new(bytes.Buffer)
            err := tpl.Execute(buf, data{values, key})
            fmt.Printf("%q\n%v\n", buf.String(), err)
      }
}

What is the expected output? What do you see instead?

Expected:
"123456789"
<nil>
"<nil>"
<nil>
"<nil>"
<nil>

Got:
"123456789"
<nil>
"<nil>"
<nil>
""
template: test:1: wrong type for value; expected *big.Int; got string

Please use labels and text to provide additional information.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions