Skip to content

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

@rhysh

Description

@rhysh

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions