Nope. Your solution doesn't work either: https://go.dev/play/p/DSik2kJ-gg4 
(it doesn't crash, just falsely returns 'false').

What I'm trying to check is pretty straightforward: do I get the nil value 
passed to a function. All types are completely valid, the structure is 
correctly cast to the base interface type and its methods can be safely 
called: https://go.dev/play/p/xseNiCSB0uA

I don't see anything "uninteresting" in that. In a real program the 
interface is simply passed through an intermediate function, so I can't use 
an exact type.

That's a stupid part of the language and there's no good justification why 
this hasn't been yet fixed.
On Wednesday, March 16, 2022 at 10:36:48 AM UTC-7 axel.wa...@googlemail.com 
wrote:

> On Wed, Mar 16, 2022 at 6:21 PM Alex Besogonov <alex.be...@gmail.com> 
> wrote:
>
>> I'm trying to solve the eternal "interface == nil" problem and I can't 
>> find a solution in this case: https://go.dev/play/p/DSik2kJ-gg4
>>
>
> ISTM that you are discovering why this check just is not an interesting 
> check to make in the first place. 
>
> There is no meaning you can assign to "is the dynamic value of a non-nil 
> interface nil", as `nil` might not even be a valid value for its dynamic 
> type. It's also not a *helpful* check, because even non-nil pointers can 
> cause a panic, if you call an associated method on them.
>
> You *can* write a check to see if a value contains a pointer relatively 
> easily using reflect:
> https://go.dev/play/p/dDHp7KnoHHS
> But it's not a very useful check, unless you specifically want to work 
> further with the pointer using reflect (e.g. how json.Unmarshal stores a 
> value in a pointer).
>  
>
>>
>> It looks like casting to the base pointer type confuses the reflection 
>> package. Is there a way to solve this?
>>
>> PS: PLEASE do add "nilptr" to the language proper.
>>
>> -- 
>> 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/469b88b2-6d64-4bcd-8b92-18f9781c4fd3n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/469b88b2-6d64-4bcd-8b92-18f9781c4fd3n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/75f9517f-c6ea-4a93-8c88-d1293b5c8c0en%40googlegroups.com.

Reply via email to