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: Cannot compare slice to nil in template using eq or ne #51642

Closed
ttc0419 opened this issue Mar 13, 2022 · 6 comments
Closed

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

ttc0419 opened this issue Mar 13, 2022 · 6 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@ttc0419
Copy link

ttc0419 commented Mar 13, 2022

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
@seankhliao seankhliao changed the title html/template: Cannot compare slice to nil in template using eq to ne text/template: Cannot compare slice to nil in template using eq or ne Mar 13, 2022
@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 13, 2022
@seankhliao
Copy link
Member

note you can do {{ if not .field }}

cc @robpike

@ttc0419
Copy link
Author

ttc0419 commented Mar 13, 2022

@seankhliao Thanks for the suggestion, but what I want to achieve is to replicate PHP's isset() function. I want to check if the slice variable is set in the template if it's set render the result html; if it's empty, show "no results" string and display the results if there are any. If I use {{ if not .field }}, both the non-exist and the empty case would be true.

@robpike robpike self-assigned this Mar 13, 2022
@robpike
Copy link
Contributor

robpike commented Mar 13, 2022

This is indeed a bug. The eq (and by extension ne) functions do not handle comparisons with nil.

@gopherbot
Copy link

Change https://go.dev/cl/392274 mentions this issue: text/template: permit eq and ne funcs to check against nil

@ttc0419
Copy link
Author

ttc0419 commented Mar 20, 2022

@robpike Hi, just wondering if the fix is included in 1.18? If not, when the fix will be landed?

@ianlancetaylor
Copy link
Contributor

The fix for this will be in the future 1.19 release.

@rsc rsc unassigned robpike Jun 22, 2022
@golang golang locked and limited conversation to collaborators Jun 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants