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: "call" function has become too picky about function types #17714

Closed
rhysh opened this issue Nov 1, 2016 · 1 comment
Closed
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@rhysh
Copy link
Contributor

rhysh commented Nov 1, 2016

The template packages have become pickier about the types of the functions they invoke with call, and less willing to peek into interface{} values to see if they contain a function. I encountered this in an application that works well with Go 1.7 but which is currently broken with Go tip.

What version of Go are you using (go version)?

$ ~/go1.8/bin/go version
go version devel +9dba338 Mon Oct 31 20:36:31 2016 +0000 darwin/amd64

What operating system and processor architecture are you using (go env)?

$ ~/go1.8/bin/go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/rhys/work"
GORACE=""
GOROOT="/Users/rhys/go1.8"
GOTOOLDIR="/Users/rhys/go1.8/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/49/zmds5zsn75z1283vtzxyfr5hj7yjq4/T/go-build147724620=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

https://play.golang.org/p/F0PUZ5thdZ

package main

import (
	"bytes"
	"log"
	"text/template"
)

func main() {
	log.SetFlags(0)

	var buf bytes.Buffer

	tmpl := template.Must(template.New("").Parse(`{{call .PlusOne 1}}`))
	err := tmpl.Execute(&buf, map[string]interface{}{
		"PlusOne": func(n int) int {
			return n + 1
		},
	})

	if err != nil {
		log.Fatalf("error: %v", err)
	}
	if buf.String() != "2" {
		log.Fatalf("wrong output: %q", buf.String())
	}
}

What did you expect to see?

I expected the script to produce no output, with exit status 0

$ ~/go1.7/bin/go version
go version go1.7.3 darwin/amd64
$ ~/go1.7/bin/go run ./main.go
$ echo $?
0

What did you see instead?

$ ~/go1.8/bin/go version
go version devel +9dba338 Mon Oct 31 20:36:31 2016 +0000 darwin/amd64
$ ~/go1.8/bin/go run ./main.go
error: template: :1:2: executing "" at <call .PlusOne 1>: error calling call: non-function of type interface {}
exit status 1
$ echo $?
1

This appears to have been broken by 5378dd7, https://golang.org/cl/31462

@dsnet dsnet added this to the Go1.8 milestone Nov 3, 2016
@rsc rsc self-assigned this Nov 7, 2016
@quentinmit quentinmit added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Nov 7, 2016
@gopherbot
Copy link

CL https://golang.org/cl/33139 mentions this issue.

@golang golang locked and limited conversation to collaborators Nov 11, 2017
@rsc rsc removed their assignment Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

5 participants