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: missingkey=zero behaves unexpectedly with map[string]any #71595

Closed
mjwhitta opened this issue Feb 6, 2025 · 4 comments
Closed
Labels
BugReport Issues describing a possible bug in the Go implementation.

Comments

@mjwhitta
Copy link

mjwhitta commented Feb 6, 2025

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 = ""
@ianlancetaylor
Copy link
Member

The text/template package prints a nil value of type any as "<no value>". You will see the same thing for a struct with a field of type any whose value is nil. I don't think that we can change that behavior now; it seems likely that it would break existing use cases.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Feb 6, 2025
@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Feb 6, 2025
@mjwhitta
Copy link
Author

mjwhitta commented Feb 6, 2025

I think it would help if the existing documentation at least mentioned this behavior, so that users don't waste time debugging. If I had searched for map[string]interface in the issues, it looks like I would have found the details I needed. But GitHub issues should not be a replacement for documentation. A simple sentence mentioning nil values will still output <no value> is probably good enough. Alternatively, could we not get something like missingkey=zeronil with new functionality? This would not break existing use cases.

@ianlancetaylor
Copy link
Member

The behavior isn't really related to missingkey or maps. It's how text/template always handles nil values of type any.

Perhaps the docs should have a section on how values are printed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation.
Projects
None yet
Development

No branches or pull requests

3 participants