On Jan 20, 2019, at 16:36, Burak Serdar <bser...@ieee.org> wrote:
> 
>> On Sun, Jan 20, 2019 at 2:32 PM 伊藤和也 <kazya.ito.dr...@gmail.com> wrote:
>> 
>> type a interface{
>>   m()
>> }
>> 
>> type abc int
>> func (abc) m() {}
>> 
>> func main() {
>>   var v a
>>   v.m()
>    ^^^^^^
> 
> v is nil. It is declared but never assigned a value. You are
> dereferencing v to call m, causing nil ptr dereference.

Importantly, v is of type a, which is an interface; it has to be dereferenced 
in some sense in order to get its type information in order to figure out what 
m() method to call.

If you’d made v of type *abc (and m() was a method for type *abc, not abc), 
this would actually be valid and would execute, but the instance pointer passed 
to m() would be nil. Sometimes that’s actually something you want to do/handle, 
though I would argue it’s fairly rare and more often it’s something you would 
want to check as an error case.


- Dave

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to