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

text/template: "call" function has become too picky about function types #17714

rhysh opened this issue Nov 1, 2016 · 1 comment
Assignees
Milestone

Comments

@rhysh
Copy link
Contributor

@rhysh 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
@gopherbot
Copy link

@gopherbot gopherbot commented Nov 11, 2016

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

@gopherbot gopherbot closed this in 39e3cbf Nov 11, 2016
@golang golang locked and limited conversation to collaborators Nov 11, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.