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
why a nil interface cannot cast to sub type pointer? #29763
Comments
For questions about Go, see https://golang.org/wiki/Questions. Or just read the language spec, which goes into detail to explain how this works and why. |
For future questions, please show a small self-contained program that demonstrates what you are asking. Go does not have any concept like "sub type pointers" so I'm not sure what you mean by that. |
do you know what i mean? |
I'm sorry, I do not know what you mean. I agree that with the code you show the initialization of var a Interface = &A{}
var b = a.(*A) and then the code would not panic. |
@ianlancetaylor |
Because a nil interface has no type. It is not the same as an interface that holds a typed nil pointer. For example, these lines will also not panic: var a Interface = (*A)(nil)
var b = a.(*A) See also https://golang.org/doc/faq#nil_error for a related issue. |
@ianlancetaylor for example, if a nil interface cast to a pointer will not panic:
but right now , i must write some code like:
the codes is short and beautiful. |
var a Interface
var b, _ = a.(*A) |
i wrote this sample. three ways to cast a nil interface to a nil pointer, none is simple. |
As said before, please take this question to the forums. This thread is going in circles and there's no issue here, so I'm going to lock it for now. |
this is a language design question
why the
1
will panic?proto.Message and HelloReplay has a stable relationship.
Why this type conversion is not allowed? Especially a
nil
to*T
nil cast.what if a empty
proto.Message
object can cast to*HelloReply
object, will the system become unsafe?but if it can, we could write less codes, we could not write useless codes for example type switch!!!
it's useful.
The text was updated successfully, but these errors were encountered: