Skip to content

text/template: Cannot compare slice to nil in template using eq or ne #51642

@ttc0419

Description

@ttc0419

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

$ go version
go version go1.17.8 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="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/william/Library/Caches/go-build"
GOENV="/Users/william/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/usr/local/Cellar/go/1.17.8/libexec/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/usr/local/Cellar/go/1.17.8/libexec"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/opt/go/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17.8"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/william/Documents/Code/Web/Web Framework Benchmark/projects/gin/go.mod"
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 x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/g_/rf2xzjmn3qs5z_b5w9m_wtbc0000gn/T/go-build131228677=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

https://go.dev/play/p/GOfZUGAcoI1

code:

package main

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

func main() {
	data := make(map[string]interface{})
	data["field"] = []string{"123", "456"}

	fmt.Println("[code] field is nil:", data["field"] == nil)

	nilTemplate, err := template.New("nil").Parse("[temp] field is nil:{{ if eq .field nil }} true {{ else }} false {{ end }}\n")
	if err != nil {
		panic(err)
	}

	err = nilTemplate.Execute(os.Stdout, data)
	if err != nil {
		panic(err)
	}
}

What did you expect to see?

[code] field is nil: false
[temp] field is nil: false

What did you see instead?

[code] field is nil: false
[temp] field is nil:panic: template: nil:1:26: executing "nil" at <eq .field nil>: error calling eq: uncomparable type []string: [123 456]

goroutine 1 [running]:
main.main()
        /Users/william/Documents/Code/Web/Web Framework Benchmark/projects/gin/temp.go:22 +0x1a5
exit status 2

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