Skip to content

text/template: cannot compare to '.' constant (byte(46)) #34483

@twpayne

Description

@twpayne

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

$ go version
go version go1.13 darwin/amd64

Does this issue reproduce with the latest release?

Not tested, but I can't find any existing issues.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/twp/Library/Caches/go-build"
GOENV="/Users/twp/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/twp"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.13/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.13/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/7m/6k5drphj6gjfgm_8gj44_55w0000gn/T/go-build109468161=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

The bug is demonstrated by this program:

package main

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

func main() {
	tmpl := template.Must(template.New("").Parse(`
		{{- if (eq .X '.') -}}
			x is a dot
		{{- else -}}
			x is not a dot
		{{- end -}}
	`))
	b := &bytes.Buffer{}
	if err := tmpl.Execute(b, map[string]interface{}{
		"X": '.',
	}); err != nil {
		panic(err)
	}
	fmt.Println(b.String())
}

Link to play.golang.org.

What did you expect to see?

I expected to see the output

x is a dot

What did you see instead?

The program panics with:

panic: template: :2:10: executing "" at <eq .X '.'>: error calling eq: incompatible types for comparison

goroutine 1 [running]:
main.main()
	/tmp/sandbox073720964/prog.go:21 +0x420

Note that if I replace the '.' constant in the template with the integer 46, then the output is as expected, as demonstrated in this link to play.golang.org. If I replace the '.' with a different character constant, e.g. 'a', then the output is as expected.

The text/template documentation states:

- A boolean, string, character, integer, floating-point, imaginary
  or complex constant in Go syntax. These behave like Go's untyped
  constants. Note that, as in Go, whether a large integer constant
  overflows when assigned or passed to a function can depend on whether
  the host machine's ints are 32 or 64 bits.

but it looks like the byte constant '.' is not treated the same way as other byte constants.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions