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: panics on method on nil interface value [1.12 backport] #30464

gopherbot opened this issue Feb 28, 2019 · 4 comments

text/template: panics on method on nil interface value [1.12 backport] #30464

gopherbot opened this issue Feb 28, 2019 · 4 comments


Copy link

@gopherbot gopherbot commented Feb 28, 2019

@mvdan requested issue #30143 to be considered for backport to the next 1.12 minor release.

@gopherbot please backport to 1.12

Copy link

@gopherbot gopherbot commented Feb 28, 2019

Change mentions this issue: [release-branch.go1.12] text/template: error on method calls on nil interfaces

Copy link

@julieqiu julieqiu commented Mar 12, 2019

@mvdan - there isn't a reason provided in the gopherbot message. Would you mind providing one for this backport?

Copy link

@mvdan mvdan commented Mar 13, 2019

We fixed one set of panics when making template calls, but forgot some others. This makes 1.12 more consistent in handling panics during template calls.

Is there a convention to add the reason for the backport in the backport issue directly?

gopherbot pushed a commit that referenced this issue Mar 13, 2019

Trying to call a method on a nil interface is a panic in Go. For

	var stringer fmt.Stringer
	println(stringer.String()) // nil pointer dereference

In we started recovering panics encountered
during function and method calls. However, we didn't handle this case,
as text/template panics before evalCall is ever run.

In particular, reflect's MethodByName will panic if the receiver is of
interface kind and nil:

	panic: reflect: Method on nil interface value

Simply add a check for that edge case, and have Template.Execute return
a helpful error. Note that Execute shouldn't just error if the interface
contains a typed nil, since we're able to find a method to call in that

Finally, add regression tests for both the nil and typed nil interface

Fixes #30464.

Change-Id: Iffb21b40e14ba5fea0fcdd179cd80d1f23cabbab
Run-TryBot: Daniel Martí <>
TryBot-Result: Gobot Gobot <>
Reviewed-by: Emmanuel Odeke <>
(cherry picked from commit 15b4c71)
Reviewed-by: Brad Fitzpatrick <>
Copy link

@gopherbot gopherbot commented Mar 13, 2019

Closed by merging ad8ebb9 to release-branch.go1.12.

@gopherbot gopherbot closed this Mar 13, 2019
@golang golang locked and limited conversation to collaborators Mar 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

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