Skip to content

text/template: pointer receivers methods do not work with non pointer values in templates #35079

@lucacasonato

Description

@lucacasonato

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

$ go version
go version go1.13.3 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/Users/***/go/bin"
GOCACHE="/Users/***/Library/Caches/go-build"
GOENV="/Users/***/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/***/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/x_/ktsmwdms0sgcdvpp051973b00000gn/T/go-build142077690=/tmp/go-build -gno-record-gcc-switches -fno-common"GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/x_/ktsmwdms0sgcdvpp051973b00000gn/T/go-build592860461=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I tried to call a method with a pointer reciever on a non pointer value in a template.

Here is an example: https://play.golang.org/p/GRqFTgTCwZh

  • Template 1 is just the control
  • Template 2 shows what doesnt work
  • Template 3 is just the control for arrays
  • Template 4 shows that this does work in arrays (presumably because arrays are more pointer than value)

What did you expect to see?

That the call to the method with a pointer reciever would behave the same in the template as in regular Go syntax. I should be able to call the method on a non pointer value.

What did you see instead?

The template execution errored out with the error template: :1:2: executing "" at <.PrintPointer>: can't evaluate field PrintPointer in type main.Data

Calling the method with a pointer reciever does work when the item is in an array.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions