question about runtime design

129 views
Skip to first unread message

awh...@gmail.com

unread,
Oct 14, 2019, 8:53:23 AM10/14/19
to golang-nuts
salam. Hi gophers can any one take a look at at this snippet of code 
and answer me why should this panic at runtime, and why the compiler 
does not catch this kind of mistakes.

Ian Lance Taylor

unread,
Oct 14, 2019, 1:53:30 PM10/14/19
to awh...@gmail.com, golang-nuts
I assume that you are asking why in your code `call(nil)` will panic
at runtime. That will pass `nil` as the argument to `call`, which
takes an argument of the interface type `is`. It then tries to invoke
a method on its argument. When called with `nil`, invoking the method
fails, because there interface is `nil` and there is no method to
call. So the program crashes.

The compiler doesn't catch this because it's fine for the `call`
function to call a method on its argument, and it's fine to pass `nil`
as the argument to a function that takes a value of interface type.
It's only in combination that these fail.

If that answer doesn't make sense, perhaps you could be more specific.
For example, tell us exactly what code you think the compiler should
catch.

Ian

awh...@gmail.com

unread,
Oct 15, 2019, 8:02:28 AM10/15/19
to golang-nuts
Thank's ian.
Reply all
Reply to author
Forward
0 new messages