-
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: prints "<nil>" for %#v when type implements GoStringer #70305
Comments
I think this is working as intended?
It's wrong for the library to expect that |
|
Can't fmt use reflection to detect when GoString has a non-pointer receiver and the value is a nil pointer? |
That's just the general rule, more specifically, lower down:
and
So |
It is debatable if the nil receiver wording applies here since the method has a non-pointer receiver. It is the fmt code that chooses to dereferenciate the pointer, it is not the function panicing. In any case, wording aside, why do you think this outcome is preferable? IMO the fact that by adding a correct GoString method you can break existing usages of nil seems very problematic. |
It's not clear that we can change this behavior at this point. It's been this way for many releases. Changing it now seems likely to do more harm than good. |
I see. Could we at least call this out specifically in the documentation? Maybe: "If the panic is caused by a nil receiver to an Error, String, or GoString method (including the case where the method is defined on a non-pointer receiver), however, the output is the undecorated string" |
Change https://go.dev/cl/627495 mentions this issue: |
Go version
1.22, 1.23, dev branch
Output of
go env
in your module/workspace:What did you do?
https://go.dev/play/p/2eq4QlnXX9e
What did you see happen?
%#v
outputs<nil>
which is not correct Go syntaxWhat did you expect to see?
I expected to get
(*time.Time)(nil)
instead of<nil>
.It looks like this happens because we have a
Time.GoString
function with non-pointer receiver; I verified that using the A and B types.One library that uses
%#v
and expects it to produce correct Go syntax is github.com/gogo/protobufThe text was updated successfully, but these errors were encountered: