Skip to content

text/template: missingkey=zero behaves unexpectedly with map[string]any #71595

@mjwhitta

Description

@mjwhitta

Go version

go1.23.6

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/gopher/.cache/go-build'
GOENV='/home/gopher/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/gopher/.go/v1.23.6/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/gopher/.go/v1.23.6'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/gopher/.cache/gvm/use'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/gopher/.cache/gvm/use/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.6'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/gopher/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1773965080=/tmp/go-build -gno-record-gcc-switches'

What did you do?

package main

import (
	"os"
	"text/template"
)

var tmpl string = "var = \"{{ .var }}\"\n"

func main() {
	t, _ := template.New("t").Option("missingkey=zero").Parse(tmpl)
	t.Execute(os.Stdout, map[string]bool{})

	t, _ = template.New("t").Option("missingkey=zero").Parse(tmpl)
	t.Execute(os.Stdout, map[string]int{})

	t, _ = template.New("t").Option("missingkey=zero").Parse(tmpl)
	t.Execute(os.Stdout, map[string]string{})

	t, _ = template.New("t").Option("missingkey=zero").Parse(tmpl)
	t.Execute(os.Stdout, map[string][]string{})

	t, _ = template.New("t").Option("missingkey=zero").Parse(tmpl)
	t.Execute(os.Stdout, map[string][]string{"var": nil})

	t, _ = template.New("t").Option("missingkey=zero").Parse(tmpl)
	t.Execute(os.Stdout, map[string]any{})

	t, _ = template.New("t").Option("missingkey=zero").Parse(tmpl)
	t.Execute(os.Stdout, map[string]any{"var": nil})
}

What did you see happen?

var = "false"
var = "0"
var = ""
var = "[]"
var = "[]"
var = "<no value>"
var = "<no value>"

What did you expect to see?

var = "false"
var = "0"
var = ""
var = "[]"
var = "[]"
var = ""
var = ""

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions