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: Printf `%d` printing functions as integers confuses newbies #12295

Closed
tv42 opened this issue Aug 24, 2015 · 7 comments
Closed

fmt: Printf `%d` printing functions as integers confuses newbies #12295

tv42 opened this issue Aug 24, 2015 · 7 comments
Assignees
Milestone

Comments

@tv42
Copy link

@tv42 tv42 commented Aug 24, 2015

From IRC: http://play.golang.org/p/AnMCdU2VM1

    w := bufio.NewWriter(os.Stdout)
    fmt.Printf("Available: %d \n", w.Available)

The fix is obvious to you and me, but the fact that a fix is needed is not obvious to an unsuspecting mind.

I wonder if that could be changed to require %p (and make %v show something that doesn't look as easily confused; ideally a string that identifies it as a function), similar to the %!d(string=Hello, playground) handling.

@adg
Copy link
Contributor

@adg adg commented Aug 24, 2015

Is there a reasonable situation where someone might want to format a function pointer as a base-10 number? It would be a shame to deny that because of a rare misunderstanding. (Have you seen this come up more than once?)

@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Aug 24, 2015
@tv42
Copy link
Author

@tv42 tv42 commented Aug 24, 2015

"Reasonable" is very subjective. Right now functions behave just like pointers, which includes %d showing them in base-10 and %v as hex. That is probably what an programmer with a C background would expect, too.

I do not necessarily mean fmt behavior should change; I'm merely documenting the problems I see more than one person having. This misunderstanding may still be rare, but I've seen more than one person succumb to it; the part that especially tricks them is when they expected to see a number, and can't tell what they're seeing is a completely different kind of a number.

Perhaps there is an alternate route, e.g. via go vet, golint, etc -- though the new gophers that suffer from this don't know of these tools either! So perhaps the "right fix" is play.golang.org integration of go vet/golint?

@adg
Copy link
Contributor

@adg adg commented Aug 24, 2015

I don't think the fmt behavior could change, even if it was desired. A vet check for this seems natural. Perhaps this CL should be about that. (wdyt @robpike?)

There's issue #7597 about adding vet to the playground, and even some CLs to make it happen. I am due to review those soon.

@robpike
Copy link
Contributor

@robpike robpike commented Aug 24, 2015

Possibly worth catching in vet but I am unconvinced. There's little point in ever printing a function value; you can't even compare function values. But it's a pretty rare case and the "newbie" argument is never compelling. A better solution might just be to teach newbies to use %T when they are confused.

Vet's job is not to catch every possible mistake.

@robpike
Copy link
Contributor

@robpike robpike commented Aug 28, 2015

Yes. I think it's such an easy thing to add to vet's existing print checker that it should be done.

@robpike robpike modified the milestones: Go1.6Early, Unplanned Aug 28, 2015
@gopherbot
Copy link

@gopherbot gopherbot commented Aug 31, 2015

CL https://golang.org/cl/14122 mentions this issue.

@robpike robpike closed this in 43a7a9c Sep 2, 2015
@golang golang locked and limited conversation to collaborators Sep 4, 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
6 participants
You can’t perform that action at this time.