Zero value of os.PathError causes panic when printing

99 views
Skip to first unread message

Brian Candler

unread,
Sep 19, 2021, 7:29:16 AM9/19/21
to golang-nuts
Is this intentional?

Result I get in browser:

&{0}
%!v(PANIC=Error method: runtime error: invalid memory address or nil pointer dereference) 

Axel Wagner

unread,
Sep 19, 2021, 7:34:40 AM9/19/21
to Brian Candler, golang-nuts
I don't know if it's intentional, but I think it's expected. The panic happens because it tries to stringify the underlying error, which is nil. I would say the creation of a packages error types are the purview of that package. I don't think you can, generally, expect that their zero values are valid.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/73b364e9-bb46-4ec6-9d3d-42e7b04772f5n%40googlegroups.com.

Brian Candler

unread,
Sep 19, 2021, 7:44:14 AM9/19/21
to golang-nuts
However, the zero value itself is printable.  It's only a pointer to the zero value which panics.

Axel Wagner

unread,
Sep 19, 2021, 8:07:44 AM9/19/21
to Brian Candler, golang-nuts
That's because the `Error` method is declared on the pointer, not the value. So `fmt` uses its reflect-based printing for structs.

Brian Candler

unread,
Sep 19, 2021, 8:49:52 AM9/19/21
to golang-nuts
Ah OK, so fmt uses Error() if it exists? Makes sense then, thanks.

BTW, I received additional (and badly-formatted) copies of your messages from "N0JfR25EemNRTjZ...@devo.memo.com", relayed via a server 149.72.164.25 which appears to be sendgrid.com / joylabs.com.  The message itself gives no way for me to unsubscribe from this service, nor does the memo.com website.  Do you happen to know anything about this?
Reply all
Reply to author
Forward
0 new messages