Skip to content

text/template: unable to use a field which is a function #4520

@remyoudompheng

Description

@remyoudompheng
What steps will reproduce the problem?
1. go run

package main

import (
      "fmt"
      "os"
      "strings"
      "text/template"
)

func T(s string) string {
      return strings.Replace(s, "o", "a", -1)
}

type S struct {
      Filter func(string) string
      Name   string
}

const tpl = `{{ .Filter .Name }}`

var t = template.Must(template.New("t").Parse(tpl))

func main() {
      s := S{Filter: T, Name: "toto"}
      err := t.Execute(os.Stdout, s)
      if err != nil {
            fmt.Println(err)
      }
}

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

Expected:
tata

Got:
template: t:1:3: executing "t" at <.Filter>: Filter has arguments but
cannot be invoked as function


go version devel +ea15e7ed6d72 Sun Dec 09 03:59:33 2012 -0500

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions