Comments (18)
Hey @efritz my fix for this bug just got merged into the fmt
package! golang/go@7c7cecc - thanks for reporting it!
from jennifer.
Sorry for the late reply - was traveling for a conference. Master version does look like it works with the counterexample perfectly. And I'm very impressed at the speed of which #26363 was authored/prioritized.
I appreciate it.
from jennifer.
Which version of Go are you using?
from jennifer.
I don't seem to be able to reproduce this in the latest Go v1.10.3... This test still passes:
func TestFmtPackage(t *testing.T) {
if fmt.Sprintf("%#v", float64(0.0)) != "0" {
t.Fatal("fmt package no longer shows float bug")
}
}
Perhaps you can show me an example that's failing for you? I can add a more defensive condition on that if... something like:
if v == float64(int64(v)) && fmt.Sprintf("%#v", 1.0) == "1" {
...
}
... that might work... but I'd like to understand the problem better first and be able to reproduce it myself...
from jennifer.
I believe the bug in the standard library comes from these lines: here and here. The block in format.go
is concerned with adding the decimal point and additional decimals when we're in the sharp
mode (e.g. using the #
modifier). However, it looks like it didn't take into account the fact that when the v
verb is used, the sharp
flag is set to false
and sharpV
is set to true
(in print.go
).
It looks like this commit changed this behaviour in the last year... but I believe it still works like this in master. From the looks of the description in the commit, %#v
was intended to be included in the change, but there's no test case for that... Perhaps @martisch would like to comment on what is expected from fmt.Printf("%#v", float64(0.0))
?
from jennifer.
I'll add a Go issue and see what the consensus is.
from jennifer.
I've added a more defensive check here and released it as v1.0.1: 1e7350a
from jennifer.
Thanks for the quick response! Here's a sample program that displays this issue as it's still present in the most recent commit.
This occurs with both go 9 and 10.
package main
import (
"bytes"
"fmt"
"github.com/dave/jennifer/jen"
)
func main() {
file := jen.NewFile("example")
file.Var().Id("x").Op("=").Lit(1e+06)
buffer := &bytes.Buffer{}
if err := file.Render(buffer); err != nil {
panic(err.Error())
}
fmt.Println(buffer.String())
}
Specifically, this generates the following output, which can't be formatted by go/format.
package example
var x = 1e+06.0
from jennifer.
Oh wow, thanks for finding this. I had no idea 1e6 would be a float64. Interesting. But also fmt.Printf("%#v\n", 1000000.0)
prints 1e+06
, so we have to be more careful. I'll do a proper fix that's even more defensive.
from jennifer.
OK I think this is better... It takes the formatted value and adds .0
only if it doesn't contain .
or e
. I've pushed the change and tagged it v1.0.2... Give that a go.
from jennifer.
I'm pretty sure this is fixed now, so I'm going to close it for now. If there'a any more problems, feel free to re-open. Thanks for reporting it!
from jennifer.
BTW it looks like my suggestion to fix this behaviour in the standard library fmt
package was accepted: golang/go#26363 (won't be fixed until go1.12 though)
from jennifer.
I'm curious about why you don't just emit %T(%#v)
as you do for the other types.
from jennifer.
Could do this but I’d prefer the code to be as clean as possible...
from jennifer.
It's generated. The issue is that the change that has gone in to fmt to fix this has broken tests for Gonum mat in a way that is difficult to fix and impossible to fix nicely.
from jennifer.
Probably best to point this out over in the issue in the golang repo. I wouldn’t expect a change like this to be entirely consequence free.
from jennifer.
FYI this project already has a work-around that works with both go 1.11
and master
: 94ad458
from jennifer.
I have just done that. Thanks.
from jennifer.
Related Issues (20)
- Better ways to manage import names and aliases HOT 5
- Get *jen.Statement from reflect.Type HOT 1
- Plain struct field tag HOT 1
- Create groups in my code? HOT 19
- [go1.19, go1.20] Indented Multiline Struct Comments have tab characters HOT 3
- example with multiple return types HOT 5
- Support `Array` for `Value` besides `Dict`? HOT 2
- Dict sorts keys by string, breaking existing/natural ordering HOT 2
- Please read: Issues for improving the readability or formatting of outputted code.
- Creating a function receiver HOT 2
- Looking for call chaining including line-feeds HOT 7
- How to generate struct parameter in function qual HOT 2
- Callback with errors?
- Support multiline string literal (backtick) HOT 2
- Feature: ability to provide custom imports sorter function
- Feature Request: Do not render [] if .Types() is called with no arguments HOT 1
- Non-int untyped numeric literal HOT 2
- How to generate complex type field definitions in Struct HOT 3
- Custom types on interface HOT 3
- How to Gennerate var()? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jennifer.