On Wed, Mar 16, 2022 at 7:02 PM Alex Besogonov <alex.besogo...@gmail.com> wrote:
> Nope. Your solution doesn't work either: https://go.dev/play/p/DSik2kJ-gg4 > (it doesn't crash, just falsely returns 'false'). > That's your original code. I assume, though, you are trying to demonstrate that this doesn't return `true` when passed a nil-slice/func/chan/map. That's by design - the function does what it is designed to do, check if an interface value contains a nil-pointer. If you want something else, write a different function. > What I'm trying to check is pretty straightforward: do I get the nil value > passed to a function. > The way to check that is `return v == nil`. What you seem to be interested in is "I want to write a function which checks if the dynamic type of a function is a pointer, slice, channel, function or map type and if so, returns if the dynamic value is nil". You can write that easily enough <https://go.dev/play/p/X_pHOrxxago>. And before you link to another "this doesn't work either": Actually state what you are interested, then. A clear statement of your problem will lead to a clear path on how to write that code. > 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. > The uninteresting part is *why* you want to answer this question. It is just unlikely to be a meaningful answer to the problem you are actually trying to solve. And you'll likely be happier with Go, if you try to adhere to its design and the patterns it lays out, instead of trying to fight them. > That's a stupid part of the language and there's no good justification why > this hasn't been yet fixed. > There are a many good justifications. Even if you disagree with the reasoning, it is there. And that kind of language won't get you very positive interactions. > 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 > <https://groups.google.com/d/msgid/golang-nuts/75f9517f-c6ea-4a93-8c88-d1293b5c8c0en%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/CAEkBMfFemEfo%2B05bNRVUgQ5frG77mb8O-_atiSWrcd87%2Bs_3EA%40mail.gmail.com.