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

fmt: '%#v' verb does not print adequate dynamic type information for numerics #11163

Closed
jhftrifork opened this issue Jun 11, 2015 · 3 comments
Closed

Comments

@jhftrifork
Copy link

@jhftrifork jhftrifork commented Jun 11, 2015

Steps to reproduce

package main

import "fmt"

func main() {
    n1 := float32(1.3167616e+07)
    fmt.Printf("%#v\n", n1)
}

Expected output

float32(1.3167616e+07)

Actual output

1.3167616e+07

Notes

An important property implied by the documentation for %#v is that I can copy-paste the output into a Go program to recreate that value (at least for serializable values, e.g. not functions). At least, the function should be injective.

This is not true for numerics, since the output drops the dynamic type information. When I take the actual output above and put it in a new Go program, it gets assigned a different dynamic type:

package main

import "fmt"
import "reflect"

func main() {
    n := 1.3167616e+07  // the output of `%#v`
    fmt.Printf("%s\n", reflect.TypeOf(n).String())
}

which prints:

float64

This has cost me significant debugging time. The %#v verb should print out everything about the value that I need to know.

@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Jun 11, 2015

You might be right, but regardless this isn't something we can change at this point. The Go 1 compatibility promise means we can't change the behavior of people's programs.

@bradfitz bradfitz closed this Jun 11, 2015
@jhftrifork
Copy link
Author

@jhftrifork jhftrifork commented Jun 11, 2015

:-(

@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Jun 11, 2015

You can use fmt.Sprintf("%T(%v)", v, v)

@mikioh mikioh changed the title '%#v' verb does not print adequate dynamic type information for numerics fmt: '%#v' verb does not print adequate dynamic type information for numerics Jun 13, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.