-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: constant overflows int in template engine for 386 builds #25833
Comments
This is working as intended. According to the spec the untyped constans are You can achieve your desired outcome by using a func instead of constants. |
This is somewhat related to #20616, in that it's awkward for the validity of a template to depend on the architecture. |
In ordinary Go code, an untyped integer constant can exceed the bounds of However, as currently implemented in That difference is not mentioned in the |
This needs a decision on whether the defect is in the implementation or the documentation. |
Shouldn't the parser seeing 60000000000 choose appropriate data type, independently from the architecture? There was mentioning of "untyped constans". Is there way of specifying constant type like: @alexd765 thanks for the hint with the functions. In my case I've changed constant to float 60.0 and called .Seconds on duration. It gives enough precision for my use case. |
Ordinary numeric constants in Go are untyped (see https://blog.golang.org/constants). They are interpreted as a specific type only when assigned to or compared with a variable.
None that I can find in the text/template documentation. |
Here is an interesting tidbit from the docs (emphasis mine):
To me, that suggests that the defect is in the implementation. |
Forget that this is gt - that's just a function that anyone can write. The real question is what happens when you call a function expecting interface{} with an ideal very large constant. I think the answer has to be that it turns into int. It would be too surprising for it to turn into int64. This affects all user-installed functions, not just gt. /cc @robpike |
I feel this is missing documentation and will address it there. |
See https://play.golang.org/p/EfqrzctYxRc. Go does this too. |
Change https://golang.org/cl/141378 mentions this issue: |
What version of Go are you using (
go version
)?go version go1.10.3 linux/amd64
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
Bug exists when program built as:
env GOOS=linux GOARCH=386 go build
AND
env GOOS=linux GOARCH=386 GO386=387 go build
AND when run in GoPlayground
It works correct in amd64 architecture environment.
What did you do?
I'm trying to compare time.Duration variable with the constant in text/template.
Variable:
duration := 10*time.Minute
Template:
{{if gt . 60000000000}}true{{else}}false{{end}}
If the code is build for 386 architecture it fails with run-time error:
template: :1:10: executing "" at <60000000000>: 60000000000 overflows int
It fails in GoPlayground too:
https://play.golang.org/p/frPKxFsZKE6
What did you expect to see?
template engine should properly evaluate constants like 60000000000 for 386 builds
What did you see instead?
60000000000 overflows int
The text was updated successfully, but these errors were encountered: