You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"bytes""fmt""text/template"
)
funcmain() {
tmpl:=template.Must(template.New("").Parse(` {{- if (eq .X '.') -}} x is a dot {{- else -}} x is not a dot {{- end -}} `))
b:=&bytes.Buffer{}
iferr:=tmpl.Execute(b, map[string]interface{}{
"X": '.',
}); err!=nil {
panic(err)
}
fmt.Println(b.String())
}
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.
- 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.
The text was updated successfully, but these errors were encountered:
Oh, this was a weird one - the template package got confused and thought that the dot meant that the value was part of a floating point literal. Fix incoming.
I'm afraid this has been broken since at least Go 1.12, and there's a temporary workaround (use a number literal instead of a rune literal), so I don't think we should backport.
What version of Go are you using (
go version
)?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
OutputWhat did you do?
The bug is demonstrated by this program:
Link to
play.golang.org
.What did you expect to see?
I expected to see the output
What did you see instead?
The program panics with:
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 toplay.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:but it looks like the byte constant
'.'
is not treated the same way as other byte constants.The text was updated successfully, but these errors were encountered: