Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
remyoudompheng opened this issue Dec 11, 2012 · 4 comments
Closed

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

remyoudompheng opened this issue Dec 11, 2012 · 4 comments
Milestone

Comments

@remyoudompheng
Copy link
Contributor

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
@remyoudompheng
Copy link
Contributor Author

Comment 1:

The original intention was to make Filter a named type with a Name method in order to
study the difference between {{ .Filter.Name }} and {{ .Filter .Name }}

@remyoudompheng
Copy link
Contributor Author

Comment 2:

Oh, I'm stupid, the documentation says a function call can only be with a method or a
function from the FuncMap, and .Filter is neither of these, "call .Filter .Name" is the
correct syntax. Maybe the error message could be more useful.

@remyoudompheng
Copy link
Contributor Author

Comment 3:

Status changed to WorkingAsIntended.

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@fabsterr
Copy link

fabsterr commented Jun 23, 2016

{{call .Filter .Name}} should work in Go1.6

@golang golang locked and limited conversation to collaborators Jun 23, 2017
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants