Skip to content

text/template: pointer to empty slice/map considered truthy #51816

@jo3-l

Description

@jo3-l

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

$ go version
go version go1.18 windows/amd64

Does this issue reproduce with the latest release?

Yes; checked with Go 1.18 and tip on the playground.

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

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Joe\AppData\Local\go-build
set GOENV=C:\Users\Joe\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.18
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Joe\AppData\Local\Temp\go-build1916548998=/tmp/go-build -gno-record-gcc-switches

What did you do?

Executed the following template using text/template:

{{ if .PtrEmptySlice }} true {{ else }} false {{ end }}
{{ if .EmptySlice }} true {{ else }} false {{ end }}

where PtrEmptySlice is &[]int{} and EmptySlice is []int{}.

Go playground link.

What did you expect to see?

false for both the pointer to the empty slice and the empty slice; the text/template documentation gives the following description for the if action:

If the value of the pipeline is empty, no output is generated;
otherwise, T1 is executed. The empty values are false, 0, any
nil pointer or interface value, and any array, slice, map, or
string of length zero.
Dot is unaffected.

While pointers to arrays, slices, maps or strings are not explicitly mentioned, the rest of the documentation sets a precedent that when it refers to an array, slice, map or string, pointers are also accepted and indirected prior to being handled. For example, while the description for the range action does not explicitly state that it handles pointers to arrays, slices or maps, in practice it also accepts items of these types.

Similarly, most built-in template functions indirect their inputs before working with them. For example, the len built-in reports 0 for both the pointer to the empty slice and the empty slice, meaning that in certain cases the output for the two if actions below differs, which is unintuitive:

{{ if eq (len $x) 0 }} empty {{ else }} not empty {{ end }}
{{ if not $x }} empty {{ else }} not empty {{ end }}

Given all this, it seems reasonable to me that pointers to empty values should also be treated as empty values, for consistency purposes. While this is inconsistent with the Go language, it is notable that the existing definition is already so; slices of length zero are not zero values for their type in Go, but are treated as such in package text/template.

What did you see instead?

true for the pointer to the empty slice and false for the empty slice.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions