Open mvdan opened 5 years ago
related: https://github.com/golang/go/issues/27768#issuecomment-423040522 I think we should point this out in the documentation.
You're right; I hadn't caught that issue as the title seemed unrelated.
Should we keep this issue open and amend the documentation?
I think the issues are slightly different (the other one started as %d not applying to pointers, which IIRC is WAI according to the documentation). So happy if this stays open and we can discuss if we should just update the documentation. With the other issue i wanted to reference the note that the implementation is deliberately not following pointers to arbitrary depth as there could be loops in pointer chains.
Those look like rules for %v
, but your example uses %s
Output using %v
: https://play.golang.org/p/G4sWTdsNIi0
{x}
&{x}
{0x40e150}
&{0x40e160}
Which makes a bit more sense. Since it's a pointer to a pointer, %v
would print with &{%p}
. Either way, I agree that the documentation should be more clear.
Change https://golang.org/cl/147997 mentions this issue: cmd/vet: fix printf false negative with nested pointers
I don't think the rules only apply to %v
- one can use them with %s
just fine, as my playground program above shows.
As far as I understand the rules for compounds they should always apply. As far as I remember the implementation it will work for all verbs except for %T and %p as those are handled specially.
Every time we try to fix fmt to be more consistent or correct, we roll back the change because it breaks things. I suspect we should just leave it alone.
I don't imagine that clarifying the docs will break any program :)
https://golang.org/pkg/fmt/#hdr-Printing reads:
It is specifically said that these rules apply recursively. However, take a look at this playground link: https://play.golang.org/p/CbrniH9q45I
It currently prints:
But I'd expect it to print:
Reading the docs carefully again, I can't find a reason why only top-level pointers would follow the "pointer to above" rule that's clearly documented.
It seems to me like either the code is wrong, or the docs need clarification. If only top-level pointers are supposed to follow the "pointer to above" rule, that should be made clear in the rules.
This issue is split from #27672. I initially thought this was a bug in vet, but then started wondering if this was a bug in fmt instead.
/cc @robpike @martisch @rogpeppe