-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: runtime: make print/println print interfaces #37642
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
Comments
Personally I really like the address print, which is very useful when debugging low level stuff, e.g. to know where the interface data is allocated, on heap or on stack. Maybe it's only me... It makes sense to print the value in panic message, but the builtin print function is mostly for debugging, so I think it is okay to be more "primitive". |
I understand this proposal was made so that this change can be discussed and considered, given that a similar change to panic behavior was done in CL 221779. Thanks for providing that background and making this proposal. My opinion is that this is not a change we should make. The rationale follows. We currently have Making this change will reduce the gap between the two function groups by making See also a comment I left on the recent panic change. |
I agree with @cherrymui and @dmitshur - print and println are for when you have nothing else (bootstrapping and debugging) and should not take on additional complexity that might fail. What if you are printing a corrupted interface value? Then print crashes if you start down this path. If you want a full-featured print, that's what fmt is for. |
FWIW, I tend to use For that use-case, I do end up writing |
Awesome, thank you all for the feedback, it matches my thoughts too. @mvdan, any comments or ideas to express here, as the proposal seems to be fizzling out because of the same reasons? |
Another difference that I just realized is that panic is a function that takes an interface value. You can't help but create an interface when you call panic, so it makes sense to take it apart for printing. In contrast, print is magical and actually gets the values you pass; it only sees an interface when you pass an interface. |
I tend to agree with the above reasoning that This is roughly what was briefly discussed in #37531. I don't feel strongly about it; I was just surprised by the need to add the code there. All in all, I'm OK with closing this proposal. @rsc raises a good point in the last comment as well. |
Thank you everyone for the discussion, I shall now close this issue. |
TL;DR: depending on if the suggestion meets the bar by necessity and usage,
given the program https://play.golang.org/p/HyZYzxbzxvf
instead of printing
it should print
Background
Coming here from a CL that I worked on over the weekend and submitted https://go-review.googlesource.com/c/go/+/221779 to address #37563 in which, given an argument
passed into panic, whereby the argument's type was derived from scalar (not composite) types i.e.
bool, complex64, complex128, float32, float64, int, int8, int16,
int32, int64, string, uint, uint8, uint16, uint32, uint64, uintptr
then we'd reflect and print out the underlying value instead of just the address so
and when run
go run main.go
Before
After
panic: main.MyString("This one")
In a comment https://go-review.googlesource.com/c/go/+/221779/8#message-96fa520e85ebf3a92c0e97e475edb8b27279ba68, @mvdan asked whether we would should do the same thing for print/println, and I replied with https://go-review.googlesource.com/c/go/+/221779/8#message-96fa520e85ebf3a92c0e97e475edb8b27279ba68
in which I express that it makes sense to implement that change for panic since panic is the only API for its use but print/println are accessible via fmt.Print APIs so not commonly used by Go developers outside of the core team (but this is just my assumption)
Question
Should we perhaps be making a change so that print/println check their interface arguments' type tags and print out the underlying value if we encounter types derived from scalar values?
What is the demand for this feature? Would it perhaps help in better debugging for the runtime, compiler and other package that can't yet access fmt.Print*, maybe because of import cycles or allocation costs? What could be the performance impact for switching on type tags?
The text was updated successfully, but these errors were encountered: