Re: [go-nuts] Need explain on error inteface work?

2016-09-16 Thread Giang Tran
got it, thank you.

On Saturday, September 17, 2016 at 12:14:34 AM UTC+7, Ian Lance Taylor 
wrote:
>
> On Fri, Sep 16, 2016 at 8:24 AM, Giang Tran  > wrote: 
> > 
> > I have a small test like this 
> > 
> > package main 
> > 
> > type MError struct { 
> > 
> > } 
> > 
> > func (m *MError) Error() string { 
> >  return "MError" 
> > } 
> > 
> > func NewMError() *MError { 
> >  return nil 
> > } 
> > 
> > func main() { 
> >  var e error 
> >  e = NewMError() 
> >  println(e.Error()) 
> > } 
> > 
> > 
> > 
> > I know that interface actually combine like (Type, value), if I change 
> > 
> > 
> > func (m *MError) Error() string { 
> > 
> > 
> >  to 
> > 
> > 
> > func (m MError) Error() string { 
> > 
> > 
> > would lead to "panic: value method main.MError.Error called using nil 
> > *MError pointer", 
> > 
> > why we have this different behave? 
>
> When you have a pointer, and call a value method, what that means is 
> that the value is copied out of the pointer and then the method is 
> invoked.  That is, when you write e.Error(), where e is type *MError 
> and Error is a value method, the actual code is something like 
> tmp := *e 
> tmp.Error() 
> But in this case e is nil, so copying out the value fails, and you get a 
> panic. 
>
> Ian 
>

-- 
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.


Re: [go-nuts] Need explain on error inteface work?

2016-09-16 Thread Ian Lance Taylor
On Fri, Sep 16, 2016 at 8:24 AM, Giang Tran  wrote:
>
> I have a small test like this
>
> package main
>
> type MError struct {
>
> }
>
> func (m *MError) Error() string {
>  return "MError"
> }
>
> func NewMError() *MError {
>  return nil
> }
>
> func main() {
>  var e error
>  e = NewMError()
>  println(e.Error())
> }
>
>
>
> I know that interface actually combine like (Type, value), if I change
>
>
> func (m *MError) Error() string {
>
>
>  to
>
>
> func (m MError) Error() string {
>
>
> would lead to "panic: value method main.MError.Error called using nil
> *MError pointer",
>
> why we have this different behave?

When you have a pointer, and call a value method, what that means is
that the value is copied out of the pointer and then the method is
invoked.  That is, when you write e.Error(), where e is type *MError
and Error is a value method, the actual code is something like
tmp := *e
tmp.Error()
But in this case e is nil, so copying out the value fails, and you get a panic.

Ian

-- 
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.