Skip to content

text/template: handling of nil keyword #58326

@mxxk

Description

@mxxk

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

$ go version
go version go1.19.5 darwin/arm64

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="arm64"
GOBIN=""
GOCACHE="/Users/user/gLibrary/Caches/go-build"
GOENV="/Users/user/gLibrary/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/user/g.asdf/installs/golang/1.19.5/packages/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/user/g.asdf/installs/golang/1.19.5/packages"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/user/g.asdf/installs/golang/1.19.5/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/user/g.asdf/installs/golang/1.19.5/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.19.5"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/kq/zqxnkcz94n97q3qrpvtl16l00000gn/T/go-build3844177246=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

These templates were evaluated to demonstrate how the argument nil is handled:

  • {{nil}}
  • {{print nil}}
  • {{nil | print}}
Source (go.dev/play/p/ZUCywuoOq7E)
package main

import (
	"fmt"
	"os"
	"text/template"
)

var templates = []string{
	// Examples using `nil`
	"{{nil}}",
	"{{print nil}}",
	"{{nil | print}}",
	// Examples using `true`, for comparison
	"{{true}}",
	"{{print true}}",
	"{{true | print}}",
}

func main() {
	for i, templateSource := range templates {
		name := fmt.Sprintf("Template %d", i)
		tmpl := template.Must(template.New(name).Parse(templateSource))
		fmt.Printf("# %s\n", name)
		if err := tmpl.Execute(os.Stdout, nil); err != nil {
			fmt.Println(err)
			continue
		}
		fmt.Println()
	}
}

What did you expect to see?

According to the text/template docs, nil is a valid standalone argument:

Arguments

An argument is a simple value, denoted by one of the following.

  • A boolean, string, character, integer, floating-point, imaginary or complex constant in Go syntax. [...]
  • The keyword nil, representing an untyped Go nil.

Based on the above, these are expected to render:

Template Expected result
{{nil}} <no value> (how nil value renders)
{{print nil}} <nil>
{{nil | print}} <nil>

What did you see instead?

Template Actual result
{{nil}} Error: "Template 0": at <nil>: nil is not a command
{{print nil}} <nil>
{{nil | print}} Error "Template 2": at <nil>: nil is not a command

If the observed behavior is actually correct/intentional, then should the docs be updated to reflect this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions