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: inconsistent pointer formatting with b/d/o/x/X verbs #65753

Open
zoltanhalassy opened this issue Feb 16, 2024 · 5 comments
Open

fmt: inconsistent pointer formatting with b/d/o/x/X verbs #65753

zoltanhalassy opened this issue Feb 16, 2024 · 5 comments
Assignees
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@zoltanhalassy
Copy link

zoltanhalassy commented Feb 16, 2024

Go version

Playground Go 1.22

Output of go env in your module/workspace:

Playground 1.22 go env

What did you do?

https://go.dev/play/p/ivQo1wTK4NM

What did you see happen?

0xc000014070
&{%!b(string=baz)}
&{%!d(string=baz)}
&{%!o(string=baz)}
&{62617a}
&{62617A}

What did you expect to see?

https://pkg.go.dev/fmt#hdr-Printing for go1.22.0 states:

The %b, %d, %o, %x and %X verbs also work with pointers,
formatting the value exactly as if it were an integer.

Either the documentation should be fixed or the behavior of fmt.

@seankhliao seankhliao changed the title fmt: Pointer formatting behavior doesn't match documentation fmt: inconsistent pointer formatting with b/d/o/x/X verbs Feb 16, 2024
@seankhliao
Copy link
Member

seems to work with primitive types like pointers to bool/string/int, but not for composite types like slices, maps, and structs.

cc @robpike

@seankhliao seankhliao added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed Documentation labels Feb 16, 2024
@robpike
Copy link
Contributor

robpike commented Feb 17, 2024

The cited documentation is irrelevant to the issue, as you can see if you try the program without taking the address of the struct: you see the same behavior.

Now the question is, should the formats recur into the fields of a struct? They do not, and doing so would rarely be the right thing to do. Whether the documentation needs work here I'm not sure, but the paragraph you cite is irrelevant, as I said.

@robpike
Copy link
Contributor

robpike commented Feb 17, 2024

Have a look at https://go.dev/play/p/HnO93o9-oao

The particular problem cited in this issue is that formats propagate for integer values but not strings, although as the example shows it would be rarely useful for them to do so.

That's probably fixable, but also probably not worthwhile.

@seankhliao
Copy link
Member

the format also works for books and strings though
https://go.dev/play/p/MSCt54fX00c
I think the expectation is that it doesn't look into the value, just print the pointer address

@zoltanhalassy
Copy link
Author

zoltanhalassy commented Feb 17, 2024

The cited documentation is irrelevant to the issue

Sorry, there might be a misunderstanding. The original title for the report was "fmt: Pointer formatting behavior doesn't match documentation". I don't have an opinion how the code should behave, just it doesn't behave as documented.

if you try the program without taking the address of the struct: you see the same behavior

A struct value is not a pointer; so the section "Pointer:" doesn't apply, and the section below that called "For compound objects, ..." applies, that's consistent.

If you think the code is working as intended, I'm fine with that. In that case I suggest the following changes to the documentation (emphasis on the changes):

Pointer:
%p base 16 notation, with leading 0x
The %b, %d, %o, %x and %X verbs also work with pointers to non-compound objects,
formatting the value exactly as if it were an integer.

and below

For compound objects, and pointers to compound objects, the elements are printed using these rules, ...

@robpike robpike self-assigned this Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants