-
Notifications
You must be signed in to change notification settings - Fork 18k
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: improve documentation about how verbs work when printing pointers #21409
Comments
Looking at the the docs this seems like a bug in fmt. |
We can't change fmt, but we could change cmd/vet. |
CC @robpike |
Even when the behavior is not supported by the documentation? |
If the behavior is deemed correct, can the documentation be adjusted to account for it? |
In some cases we can adjust the doc, but in a case like this I personally think that the chances of breaking a currently working program are too high. |
Sorry, I mean in some cases we can adjust the code (but I think this case is too risky). We can always adjust the doc, and, yes, we should do so. |
I can't find it now but I seem to remember this was an actual change that went in at some point, and that the docs need to catch up to the change. I might just be confused though. |
According to the documentation, section 'Format errors', passing a pointer to the %d verb is the case of invalid argument, %d is documented to accept only integers - and it should produce Can someone please explain to me why adjusting the docs instead of fixing the bug is considered? IMO Also, for the %d verb to accept pointers looks to me like a C-ism and the OP readily demostrates why it's good that Go avoids them. |
AFAICT %d has been supported for pointers since go1.0: The verbs b, o, x, X are also supported on pointers and not listed explicitly in the pointer section. We have refrained from changing other behavior in fmt after go1 to avoid breaking code even if it is inconsistent: e.g. byte vs uint8 printing for types and # not always taking into account padding. This is i think rather a documentation bug than a bug in fmt as go1 code clearly intended these verbs to work on pointers but did not document it. Therefore, i would suggest we document the support for other verbs than p on pointers more explicitly then removing the b, o, d, x, X verb support for pointers. @robike maybe this is the change you were searching for: this changed the behavior of %d on pointers for go1.1 but did not introduce the %d support for pointers. |
Thanks for the link. It makes clear that supporting %d and other verbs for pointers is intentional. IIUC now, it's undocumented behavior since Go 1. Programs relying on undocumented behavior are not protected by the Go 1 compatibility promise. |
@martisch Yes, that's the one I was thinking of. I agree it's a documentation issue at this point. |
Change https://golang.org/cl/59412 mentions this issue: |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go1.9rc2 linux/amd64
What operating system and processor architecture are you using (
go env
)?GOHOSTARCH="amd64"
GOHOSTOS="linux"
What did you do?
https://play.golang.org/p/k89vjAT5Kt
package main
import (
"fmt"
)
func main() {
i := 37
p := &i
fmt.Printf("Why: %d", p)
}
What did you expect to see?
I expected to see some sort of error / warning about the implicit conversion of a pointer type into an integer format verb %d. I expected the computer to help me catch the mistake of not derefrencing p to get the expected integer.
What did you see instead?
No error or warning, and the output contains the address of the pointer printed in base10.
The text was updated successfully, but these errors were encountered: